Skip to content

[5.3] add $default parameter to query builder when() method #15422

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

Closed
wants to merge 183 commits into from

Conversation

tomschlick
Copy link
Contributor

Originally opened in #15421 but @themsaid asked I open it against master

This allows you to specify a default closure to run if the when() $value attribute is false.

I recently found myself creating a sorting / filtering system with defaults that the user could change. Without this you would have to do all kinds of wackiness to clear the previous where() clause on $value being true.

Here is an example of this in action

Time::when($request->has('type'), function ($query) use ($request) {
    return $query->where('type', $request->input('type'));
}, function ($query) {
    return $query->where('type', 1);
})
->get();

@tomschlick
Copy link
Contributor Author

Looks like composer is having some issues on Travis... Any way to schedule a rebuild on that?

@themsaid themsaid closed this Sep 14, 2016
@themsaid themsaid reopened this Sep 14, 2016
@taylorotwell
Copy link
Member

Do you want to send this to 5.3?

@tomschlick tomschlick changed the base branch from master to 5.3 September 14, 2016 13:25
@tomschlick
Copy link
Contributor Author

Ugh, this branch switcher from GH sucks. I'll re-submit.

@tomschlick tomschlick closed this Sep 14, 2016
@themsaid
Copy link
Member

@tomschlick I was the reason for all this opening/closing hassle, sorry :)

@tomschlick
Copy link
Contributor Author

No problem haha

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