Eloquent ORM Code Hinting in PhpStorm
For future Googlers, and perhaps OP as well if you are still sticking to Laravel.
The laravel-ide-helper package solves this issue for you quite elegantly, with what I believe is a relatively new feature; generated model PHPDocs.
You can generate a separate file for all PHPDocs with this command:
php artisan ide-helper:models
The generated metadata will look something like this for each class:
namespace App {
/**
* App\Post
*
* @property integer $id
* @property integer $author_id
* @property string $title
* @property string $text
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \User $author
* @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
*/
class Post {}
}
This caused issues for me in PHPStorm however, where the software was complaining about multiple class definitions. Luckily an option is readily available for writing directly to the model files:
php artisan ide-helper:models -W
There are a few more options and settings available if you need to tweak the behavior, but this is the gist of it.
PHPStorm - Laravel Eloquent no code hinting
You should use laravel-ide-helper package (https://github.com/barryvdh/laravel-ide-helper).
Install:
composer require --dev barryvdh/laravel-ide-helper
Hinting for models:
php artisan ide-helper:models
php artisan ide-helper:models --reset
Use --reset option to replace whole phpdoc block in models.
Code hinting for facades methods:
php artisan ide-helper:generate
Code hinting for classes called through containers:
php artisan ide-helper:meta
How can I provide hints to PhpStorm to find my Laravel custom query builder methods?
You'll need to add the class methods to the model using the unofficial @mixin
PHPDoc directive. PhpStorm has had support for it for a few years now. A trait is probably the easiest way to do this on multiple models:
/**
* @mixin MyModelQueryBuilder
*/
trait HasCustomBuilder {
public function newEloquentBuilder($query): MyModelQueryBuilder
{
return new MyModelQueryBuilder($query);
}
}
class MyModel extends Model {
use HasCustomBuilder;
}
How to navigate Laravel model scopes in PhpStorm
as LazyOne said
- There is no real method emailLike() -- it is magic one (during runtime Laravel uses scopeEmailLike() instead). Try declaring such method using @method in a PHPDoc for the class. 2) Consider using Laravel Idea plugin -- it's a paid one BUT it offers A LOT of features for Laravel development, especially for code completion. And it's in active development. At very least give it a try.
Type hinting for the model objects of Eloquent ORM
Your best bet would be to install the laravel-ide-helper ServiceProvider package in your application
https://github.com/barryvdh/laravel-ide-helper
Seems to have a lot of helpful features for type hinting to the IDE.
PhpStorm cannot find model's methods in Laravel
Due to laravel "magic" Ide have some hard time to figure out what's going on,
I recommend you to use this :
https://github.com/barryvdh/laravel-ide-helper
just follow the instruction and your ide will now have a better understanding of the framework.
You can also install some PHPStorm plugins related to laravel :)
PHPStorm auto-complete not working with Laravel 5
Two possible fixes for that:
- Make your models extend the \Eloquent facade instead of
Illuminate\Database\Eloquent\Model. - If you prefer to keep using the
"Model" facade, you can make your own alias in config/app.php,
then change "eloquent" to "model" in the config/ide-helper.php under
extra. This will let ide-helper include all the methods from
Illuminate\Database\Eloquent\Builder and
Illuminate\Database\Query\Builder which is where the missing methods
actually live.
(Source: https://laracasts.com/discuss/channels/general-discussion/phpstorm-thinks-modelwhere-doesnt-exist-on-model-l5/replies/37661)
Related Topics
How to Replace Text Urls and Exclude Urls in HTML Tags
How to Validate Google Recaptcha V3 on Server Side
How to Access MySQL Result Set Data With a Foreach Loop
Intl Extension: Installing PHP_Intl.Dll
How to Generate a Screenshot of a Webpage Using a Server-Side Script
Serializing and Submitting a Form With Jquery and PHP
Can You Store a Function in a PHP Array
How to Execute PHP That Is Stored in a MySQL Database
When to Use Strtr VS Str_Replace
PHP: Check If Any Posted Vars Are Empty - Form: All Fields Required
PHP Header Redirect Not Working
Yii2 Htaccess - How to Hide Frontend/Web and Backend/Web Completely