Please take a look at this post: Dedicated query builders for Eloquent models
It's actually a pretty nice approach, for encapsulating scopes in a dedicated file, doesn't mess with the accessors/relationships and other logic methods.
The author gave a solution for auto-completions, as you can in the "Static analysis" paragraph, but it comes with a cost of overriding the query method in the model, and to create a query builder only by the query method.
It's a big price for an exists project.
WDYT about adding support to this kind of approach and add phpDoc for these dedicated Builder methods?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.
I'm not really sure what this means. I guess you could just add a @mixin for the Builder, but then you'd need to run the generation on the Builder class itself.
This seems a bit of an edge case, not really sure.
@barryvdh Thanks for the reply Barry. The @mixin works great but with one issue, the model inherits the methods as non-static. So it will autocomplete User::online() (which is great!), but will also autocomplete (new User)->online() (for the example, online is a scope that moved to UserBuilder)
BTW, the workaround solution for this to not use @mixin but to use * @method static UserBuilder|\App\User query(), but then you must add call ::query() at the beginning of every query.
Another solution is that the package will automatically put the UserBuilder's methods in the User phpDoc as a static method, just like the regular scopes.
I think the approach of creating a separate class for a model's Builder is really cool, first because of the encapsulation, the model won't be so fat and messy with many different things ($fillable, $casts, relationships, accessors, instance's methods, constants, etc...)
And second because finally you can have a real autocomplete inside closures, for example:
Country::whereHas('users', function (UserBuilder $query) {
$query->online(); // <-- the `online` method got autocompleted
})->get();
Or
Country::withCount([
'users as online_users_count' => function (UserBuilder $query) {
$query->online();
},
])->get();
Is there an update on this? Is it too complicated to implement?
I am using this package in my project and most of my models have a dedicated eloquent builder, so we can separate the query logic from the model.
Hello @mfn,
When you have the time, would you be able to close this issue since https://github.com/barryvdh/laravel-ide-helper/pull/1089 is already in the new release?
@kyosifov roger, thank you!
Most helpful comment
Is there an update on this? Is it too complicated to implement?
I am using this package in my project and most of my models have a dedicated eloquent builder, so we can separate the query logic from the model.