WhereAll and WhereAny In Laravel 10.47 Update
Today I'm going to share an update of the Laravel 10.47 version in which we have two new Laravel Eloquent methods that make conditions easier to writ...
Today I'm going to share an update of the Laravel 10.47 version in which we have two new Laravel Eloquent methods that make conditions easier to write most of the time in case of searching for something. We going to see how we can use these methods.
Before explaining the methods I'm gonna show you that If I use the search input box in my laravel app to search for something in DB. It can be by name, email, age, etc. I used to do something as mentioned below in the code.
// a simple search query
$orders->where('email', 'like', "%$search%")->where('name','like',"%$search%");
But now you can enjoy this new method called whereAll
instead.
WhereAll Method
This method can replace this whole line of code and can act easily in one syntax as mentioned in the below code.
// syntax
whereAll($columns, $operator = null, $value = null, $boolean = 'and')
//Usage
$orders->whereAll(['email','name'],'like',"%$search%");
//Query
select * from "orders" where (name like "shantu" AND email like "shantu")
WhereAny Method
whereAny()
method adds or
instead of and
expression in a query.
// syntax
whereANy($columns, $operator = null, $value = null, $boolean = 'and')
//Usage
$orders->whereAny(['email','name'],'like',"%$search%");
//Query
select * from "orders" where (name like "shantu" OR email like "shantu")
I'm gonna link the original commit here Add whereAll
and whereAny
methods to the query builder
I hope you find this update helpful.