laravel-ide-helper: Laravel 5 Model query methods not working

Eloquent model methods like findOrFail() and where() are not working in Laravel 5. These methods don’t exist in the Model class so the IDE can’t see them. The class uses __call() to create a Illuminate\Database\Eloquent\Builder object where those methods exist. Adding @method static \Illuminate\Database\Query\Builder|\App\Models\ModelName findOrFail($value) to the phpdoc lets the IDE know the methods are there. Can these be added to the docblock generated by the artisan ide-helper:models command?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 18 (4 by maintainers)

Most upvoted comments

Laravel use class_alias to pretend that Eloquent is the same as Illuminate\Database\Eloquent\Model. Your IDE doesn’t understand that, so the ide-helper generates a file with all the facades, where they extend the real class: Illuminate\Database\Eloquent\Model

Most facades are more of a shortcut, you can call static methods on them that get sent to some IoC object. But Eloquent is just a real alias. But the problem is that the Model (https://github.com/laravel/framework/blob/5.0/src/Illuminate/Database/Eloquent/Model.php) doesn’t even have a where method, but some magic __call methods to redirect it to the query builder. That is why we add some extra methods to the fake class.

class Eloquent extends \Illuminate\Database\Eloquent\Model{
    public static function where($column, $operator = null, $value = null, $boolean = 'and'){
        return \Illuminate\Database\Eloquent\Builder::where($column, $operator, $value, $boolean);
    }
}

They don’t exist on the actual class, but the ide-helper fakes it. If the Model would have proper phpdocs it wouldn’t be needed.