Skip to content
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

Merged
merged 1 commit into from
Jun 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,28 @@ protected function parseNestedWith($name, $results)
return $results;
}

/**
* Add the given scopes to the current builder instance.
*
* @param array $scopes
* @return mixed
*/
public function scopes(array $scopes)
{
$builder = $this;

foreach ($scopes as $scope => $parameters) {
if (is_int($scope)) {
$scope = $parameters;
$parameters = [];
}

$builder = $builder->callScope('scope'.ucfirst($scope), (array) $parameters);
Copy link
Contributor

@acasar acasar Jun 20, 2016

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

working on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return $builder;
}

/**
* Apply the given scope on the current builder instance.
*
Expand Down