-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[11.x] Add QueriedBy attribute #53176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Let me sit on this one a bit. I feel like custom query builders are much, much more rare than custom collections, which are also rare I think. 😅 |
I really hope this gets re-opened. I would love to see this in. I use custom eloquent builders in every project. |
@taylorotwell at least for those of us who follow @timacdonald for enough time, I guess dedicated query builders are way more common than custom collections, based on this post https://tim.macdonald.au/dedicated-eloquent-model-query-builders I do use 3 custom collections, on a single project. Mostly because there are lots of aggregated reports with different filters on the same view, that benefit from custom filters on those collections. But since that post came out, in 2019, all of my projects use dedicated query builders for almost every single model. |
I also wanted to chime in with my experience. I've never made a custom Collection while I heavily use custom Builders---the primary reason is for better static analysis / IDE autocompletion and to slim down the models by moving the scopes to the builders. Larastan does support both custom Collections and Builders, but there's a lot more tests, logic, and discussions centered around the custom Builders |
$reflectionClass = new ReflectionClass(static::class); | ||
|
||
$attributes = $reflectionClass->getAttributes(QueriedBy::class); | ||
|
||
if (! isset($attributes[0]) || ! isset($attributes[0]->getArguments()[0])) { | ||
return; | ||
} | ||
|
||
return $attributes[0]->getArguments()[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of looking at the arguments (and checking if they are set), you can just directly get the attribute instance and access the property:
$attribute = (new ReflectionClass(static::class))->getAttributes(QueriedBy::class)[0] ?? null;
return $attribute?->newInstance()->builderClass;
I would also use this a lot more than So, I hope you can reconsider this Taylor. |
Stumbled upon this pull request as with the recent addition of Fingers crossed on this to be reopened. |
As seen most people discussing here are aware of the pros, but I just wanted to add my two cents as well to emphasize that adding custom query builders is more than some neat sugar feature with little value that nobody uses. I have been using custom query builders for years now as well as custom collections, but if I had to decide which one of them to choose I would go for the builders all the time. That being said, I wouldn't actually need the attribute for this, it would just be the logical step since CollectedBy is already there. |
Yes, it just doesn't make sense to include attributes like What is the main arguments against adding this? The maintenance burden? Performance? Wouldn't that apply to the three others as well? |
At the risk of piling on... I use tons of custom query builders and no custom collections. I would love having this attribute in the core framework. Hoping you'll reconsider @taylorotwell. |
My understanding is that Taylor usually doesn't look at closed PR's, so I think someone needs to send in a new PR and summarize everything that have been added here since it's closing. If you want him to take another look that is. |
Along the same vein as the recently added
CollectedBy
attribute (#53122), I propose to add theQueriedBy
attribute which allows users to configure which Builder class to use without having to override thenewEloquentBuilder
method.The implementation is basically a carbon copy of the
CollectedBy
attribute, with memoizing via a static array variable on the Model class.