Home > Blockchain >  How can I provide hints to PhpStorm to find my Laravel custom query builder methods?
How can I provide hints to PhpStorm to find my Laravel custom query builder methods?

Time:12-16

I'm using custom query builders in Laravel like this:

class MyModel extends Model {
 public function newEloquentBuilder($query): MyModelQueryBuilder
    {
        return new MyModelQueryBuilder($query);
    }
}
class MyModelQueryBuilder extends Illuminate\Database\Eloquent\Builder {
  // various query methods...
}

Because of Laravel's so-called Facades and use of magic methods, PhpStorm cannot find references to the methods in my custom query builder.

I use the barryvdh/laravel-ide-helper package to generate hints for my Models, so their methods are findable and hintable. How can I do the same thing in an easy, maintainable way for my query builder classes?

For example, I'd like to be able to press Command-B while on a query builder method, and get the list of users of that method, for example, as I can with other fully cross-referenced classes.

CodePudding user response:

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;
}
  • Related