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.4] Pass the condition value to "when" #18419

Merged
merged 1 commit into from
Mar 19, 2017

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Mar 19, 2017

So that this:

$role = $request->input('role');

$users = User::when($role, function ($query) use ($role) {
    return $query->where('role_id', $role);
})->get();

becomes this:

$users = User::when($request->input('role'), function ($query, $role) {
    return $query->where('role_id', $role);
})->get();

@taylorotwell taylorotwell merged commit 36b2550 into laravel:5.4 Mar 19, 2017
@taylorotwell
Copy link
Member

Kinda clever.

@JosephSilber JosephSilber deleted the pass-when-condition branch March 19, 2017 18:51
@CristianLlanos
Copy link

Cool! Thanks 👍

@juukie
Copy link
Contributor

juukie commented Mar 22, 2017

Love it! Really laravelish

@Ilyes512
Copy link
Contributor

This should have been added to the documentation. Came here through http://laraveldaily.com/less-know-way-conditional-queries/?utm_content=buffer39804&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer I am on my phone now. If I get time later I will make a PR to the docs.

@jadamec
Copy link

jadamec commented Aug 22, 2019

Still not in docs..?

@Ilyes512
Copy link
Contributor

I am again on my phone 😬

I will (again) try to look at it in a few minutes once I am behind my desk :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants