Introduction

While working on a Laravel codebase, I often encounter errors in my IDE, even though the methods I’m using are documented in the Laravel documentation. Here are some examples of the errors I’m referring to:

Static Method Errors

Model Method Errors

Why this happens

Long story short, PHP Language Server Protocols (LSPs) like Intelephense or PHPActor do not recognize Laravel’s ‘magic methods.’ This is not the fault of the LSPs; instead, it is due to the inherent difficulty in detecting these methods because some of them are generated at runtime. It is Laravel’s responsibility to provide accurate PHPDocs so that they will be recognized by the LSP.

Laravel IDE Helper to the rescue

Laravel IDE Helper is a package that generates helper files, allowing your LSPs to recognize Laravel’s ‘magic methods.’ It not only helps eliminate errors but also enhances code completion, providing a better development experience. In my opinion, this package is essential for every Laravel project. I would argue that it should be considered as a part of the Laravel framework itself. I recommend reading the README in the GitHub repository, as it offers valuable information on its usage.

Installing Laravel IDE Helper

  1. To install, just Run this command in your project:
composer require --dev barryvdh/laravel-ide-helper

Usage

  1. To fix the errors for the Laravel Helpers like the Redirect helper from the image I provided, you can simply run:
php artisan ide-helper:generate
  1. To fix the errors in the model like the whereCode function from the image I provided, run:

Note: You have to run this command every time you create a new model

php artisan ide-helper:models
  1. If you’re using PHPStorm as an IDE, you can run:
php artisan ide-helper:meta

Some recommendations (Optional)

  1. I prefer to keep my code clean and avoid cluttering it with generated PHPDocs. As a result, I always enable the option to store the generated PHPDocs in separate helper files.
  2. I avoid versioning generated helper files, so I include them in the .gitignore file. The drawback of this approach is that when someone clones the project, they’ll need to run the generation commands again. For me, the inconvenience of running a few commands is preferable to the complexities that can arise from versioning a generated file and potentially encountering merge conflicts. If you find my approach compelling, you can simply add these files to the .gitignore configuration:
_ide_helper.php
_ide_helper_models.php

Benefits

The annoying errors should now be gone and on top of that, you get to have auto-completion of methods that are not available before, how cool is that?

Auto Completin Image

Conclusion

Laravel IDE Helper is an essential package for Laravel development. It offers auto-completion and helps eliminate errors related to Laravel’s ‘magic methods.’ It’s important to note that while it significantly reduces errors, not all of them will disappear. In my experience, a few errors may still persist, but using this package is a substantial improvement over not having it installed.