-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.2] Add method to implement multiple scopes with variable names #14049
Conversation
I don't see how using an array is in any way better than calling them directly. |
$builder = $this; | ||
|
||
foreach ($scopes as $scope => $parameters) { | ||
if (is_numeric($scope)) { |
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.
Maybe this should be is_int
?
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.
sure
I have something like this in my code:
Based on the request I determine the dynamic scope(s) to filter the posts including its parameters. Doing the same with chained methods is not that easy and don't look that nice. |
Then this is something very unique to your app, and you should keep it there. Doesn't make much sense in the framework IMO. |
Hmmm, I think it could be quite useful. Could make for nicer design rather than making method calls using variables as method names. I'm 👍 here. |
Can you send a PR for a test for this? |
$parameters = []; | ||
} | ||
|
||
$builder = $builder->callScope('scope'.ucfirst($scope), (array) $parameters); |
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.
@sileence This line doesn't work. callScope
needs callable
type as the first argument, so it should be:
$callable = [$this->model, 'scope'.ucfirst($scope)];
$builder->callScope($callable, (array) $parameters)
Also, scope can return anything, not necessarily a builder instance, so I wouldn't re-assign it here.
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.
working on that.
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.
Proposal to add the method
scopes
to allow the implementation of multiple variable scopes at once, for example:Post::scopes(['question', 'pending' => true])->get()
is the same as:Post::questions()->pending(true)->get()