How to get code completion intellisense in Laravel resources

Last updated 5th September, 2023

What is Code Completion

As a developer, staying competitive requires minimizing non-value added work. Utilizing IDE features such as autocompletion and intellisense can significantly increase productivity by flagging syntax errors, suggesting next steps, and reducing the need to search for specific properties or methods within a class.

The Laravel framework does a lot of work to make developer experience excellent, a lot of that includes spicy magic under the hood to make things as smooth as possible. In places however, that can cause confusion for your IDE. Let's look at a basic example.

Basic Example Application

Let's assume we have a basic blog. We can first scaffold some code to get that set up.

class BlogController extends Controller
{
    public function show(Request $request, Post $post): JsonResponse
    {
        return response()->json(new BlogResource($post))
    }
}

This is a fairly typical Laravel controller that you might see when developing an API with Laravel. We've got one method - show that accepts a route identifier and finds the associated Post model.

We then pass that model to a BlogResource class, that will format this model for response to the client.

class BlogResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'content' => $this->content,
        ];
    }
}

In this resource, you'll see something a little odd. When it's formatting data from the model into the format we want, it gets properties from the model by accessing them on the current class.

What Laravel is doing under the hood is forwarding these requests onto the model, which we passed through earlier.

This is great, it works and means we have a super simple solution to mapping model data.

There is one downside however: your IDE might not know how to deal with this.

In PhpStorm, you'll likely see something like this when you hover over the properties you're trying to access:

A screenshot of a PhpStorm file showing a magic method error

This is because PhpStorm doesn't know what a resource is, or that Laravel is doing this behind the scenes. You will likely see something similar in VsCode, and other comparable IDEs.

The Solution - Using Mixins To Teach Your Editor

In PHP you can use the @mixin DocBlock to tell our IDEs that all of the properties and methods included in that class are available on the class that the DocBlock is being added to.

So given our example above, that would look like the following:

/**
 * @mixin BlogPost
 */
class BlogResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'content' => $this->content,
        ];
    }
}

Simple enough right?

A screenshot of the PhpStorm IDE showing Laravel resource auto-completion

Now, when we look in PhpStorm, we can see that our IDE is picking up on the properties from the post.

If you're interested in improving your IDEs ability to pick up on more Laravel magic, I highly recommend checking out BarryVdh's Laravel IDE Helper package. It gives your IDE insights into the routes, config and models of Laravel's framework, to name a few. Its a Lifesaver! 🤙

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.

Related Articles

  • Checking The Active Route In Laravel 10

    Updated, 12th November, 2023

    It's common practice to hinge logic off which route or page is active in the user's browser. Here's a few easy ways of checking the current route to power your app functionality off.

  • Laravel uses a lot of PHP magic under the hood, generating IDE Helpers for your models helps you stay productive in your development. Here, I'll detail how to setup automated IDE generation after you run your migrations.