Skip to content

[5.4] Pass the condition value to "when"#18419

Merged
taylorotwell merged 1 commit intolaravel:5.4from
JosephSilber:pass-when-condition
Mar 19, 2017
Merged

[5.4] Pass the condition value to "when"#18419
taylorotwell merged 1 commit intolaravel:5.4from
JosephSilber:pass-when-condition

Conversation

@JosephSilber
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Member

Kinda clever.

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

Cool! Thanks 👍

@juukie
Copy link
Copy Markdown
Contributor

juukie commented Mar 22, 2017

Love it! Really laravelish

@Ilyes512
Copy link
Copy Markdown
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
Copy Markdown

jadamec commented Aug 22, 2019

Still not in docs..?

@Ilyes512
Copy link
Copy Markdown
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