Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading