Laravel AI-Generated Code with GitHub Copilot: 4 Personal Examples

Everyone is talking about AI these days, so I decided to showcase 4 examples of how GitHub Copilot tried to auto-complete or generate some code for me.

The main thing is this: you start typing something in your IDE (PhpStorm, in my case) and "suddenly" see that Copilot suggests finishing the code for you, and you can accept it in one click. Here are the examples I encountered.


Example 1. Generated Test Based on Another Test

This was generated as a suggestion by Copilot:

It used an already existing file to get the pattern:

It resulted in a similar-looking test being automatically generated.

The issue with the suggested code was that I was using the refreshDatabase trait, which meant that user ID 1 might not exist. Solved by adjusting the test:

it('can update a bank account', function () {
$user = User::factory()->create();
$bankAccount = BankAccount::factory()->create();
 
$this->actingAs($user)
->put(route('bank-accounts.update', $bankAccount->id), [
'name' => 'Test Bank Account',
'user_id' => $user->id,
])
->assertRedirect(route('bank-accounts.index'));
 
$this->assertDatabaseHas('bank_accounts', [
'name' => 'Test Bank Account',
'user_id' => $user->id,
]);
});

Example 2. Suggested Migration Column

Copilot looks at what others did and gives you suggestions:

It wasn't exactly what I wanted to do in that case, but cool to have such an example.


Example 3. Incorrect Model Suggestion

This is an example of invalid suggestions by AI. Sometimes it's definitely not correct:

In this case, I needed the Position::pluck('name', 'id') but got a random Task::pluck() suggestion. No idea why.


Example 4. Generated HTML/CSS/Tailwind Table

It also gives suggestions for Blade templates. In this case, I just typed <table, and it did the rest:

While it wasn't correct and didn't match my structure - now I don't have to type the styling myself!

This last example is probably the most useful regarding how many keystrokes of typing it saved me.


What were your examples when AI co-piloted your code and generated something for you? Were you happy with its suggestions?

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials