Eloquent


Eloquent Helper Code Generation

Eloquent is full of magic, so PhpStorm needs some helper code to understand it. There are 2 ways to do it:


Generate Eloquent Helper Code command is available in the Laravel Idea code generations:


  • Tables and columns can be fetched from migrations or from an existing database connection.
  • Model query style is about how eloquent queries will be started. The most popular way is using pseudo-static methods like User::whereEmail() and with the first option, the generator will generate these helper static methods. Some developers use Eloquent model objects, so methods are dynamic: $user->whereEmail(). The second option is for these projects. However, if another style is chosen, needed methods can be completed by double Ctrl-Space.
  • The next checkbox adds a helper code for supporting Collection proxies for all Eloquent models.
  • Eloquent adds "_count" properties for entities fetched by "withCount" method: User::with_count('posts')->first()->posts_count. You can add these properties to the helper code.
  • The last option will remove all phpDocs from models. Clear ide-helper phpDocs, for example.

Completions

After generating a helper code Laravel Idea starts to complete fields and relations almost everywhere in the strings:


Even in complex cases, it understands that here it should complete Post class fields:


Relations chain is also not a problem:


Fields inside class:

Code generations

JSON resource generation is accessible from the main Code generation menu or in the Generate menu in the Eloquent class (Alt-Ins or Command(Ctrl)-N). It uses fields and relations information to generate full JSON resource for this class:


Database factory generation is accessible from the main Code generation menu or in the Generate menu in the Eloquent class (Alt-Insert or Command(Ctrl)-N). It uses fields and relations information to generate full database factory for this class:

Database factory type hinting

Laravel Idea hints the type of factory()->create() calls:


Multiple create also supported: