PHPStorm is not recognizing methods of my Model class in Laravel 5.0 [duplicate]
If you want a class extending Model
to recognize Eloquent methods, just add the following in the top PHPDoc comment on the class:
@mixin Eloquent
Example:
<?php namespace App;use Carbon\Carbon;use Eloquent;use Illuminate\Database\Eloquent\Model;/** * Post * * @mixin Eloquent */class Post extends Model {
Edit Laravel 6+
use Illuminate\Database\Eloquent\Builder;/** * @mixin Builder */
Note:Most of you probably are using ide-helper for Laravel, therefore this @mixin
attribute is automatically generated for model Classes.
Since methods where
, latest
, find
, findOrFail
and others does not exist in Model
class, but in Builder
and are loaded via magic methods, the IDE can not detect these.
While the widely suggested laravel-ide-helper is great, it does not help also. There are multiple issues and discussions and workarounds on the subject, but all have its own problems.
Best solution I've found so far, IMHO is to downgrade severity if __magic methods are present in class. PhpStorm has this exact option in inspections settings.
Check in Settings -> Inspections -> PHP -> Undefined -> Undefined method
This will not let you click on the method, but merely disables the annoying markup. Read more about severities or check this more expressive SO answer
For anyone who came here for a solution, what worked for me is the solution in this StackOverflow:
PhpStorm laravel 5 method not found
specifically when I ran:
Edit:to use this command you have to install ide-helper, run:
composer require --dev barryvdh/laravel-ide-helper
...
php artisan ide-helper:models
and answered "yes"
after that methods are recognized.