Skip to content

[12.x] Add whereIf() method to Eloquent Builder #54810

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 1 commit into from

Conversation

flpdev
Copy link

@flpdev flpdev commented Feb 26, 2025

This PR introduces the whereIf() method in Illuminate\Database\Eloquent\Builder.

🔹 Purpose:

The whereIf() method allows conditionally applying a where() clause based on a boolean condition.
This improves readability and eliminates the need for using when() for simple conditional filters.

🔹 Usage:

// If condition is true, it applies the where clause
User::whereIf(true, 'status', '=', 'active')->get(); 
// Equivalent to ->where('status', '=', 'active')

// If condition is false, it does NOT apply the where clause
User::whereIf(false, 'status', '=', 'active')->get(); 
// Equivalent to User::query()->get() (no additional filter)

@shaedrich
Copy link
Contributor

There have already been two tries via #53303 (whenWhere()) and #54312 (whenGrouped()) this year to implement such functionality, so I doubt, this one has better chances

@crynobone crynobone changed the title Add whereIf() method to Eloquent Builder [12.x] Add whereIf() method to Eloquent Builder Feb 27, 2025
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

3 participants