Laravel eloquent multiple WHERE with OR AND OR and LIKE?
Use like
and not like
with %
in where()
and orWhere()
methods:
->where('column', 'like', '%pattern%')
https://laravel.com/docs/5.3/queries#where-clauses
If you need to use multiple AND
do like this:
->where($condition)
->where($anotherCondition)
If you want to use OR
do this:
->where($condition)
->orWhere($anotherCondition)
To combine multiple AND
and OR
do parameter grouping:
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
In this scenario Parameter Grouping Should be pretty much useful to use combination of multiple AND & OR. Here are some demo examples of how it can be implemented.
if ($request->search) {
$users = User::where('type', '=', 'Customer')
->where(function ($query) use ($request) {
$query->where('name', "like", "%" . $request->search . "%");
$query->orWhere('mobile', "like", "%" . $request->search . "%");
})->get();
}
It depicts:
select * from users where type = 'Customer' and ([name as searched] or [mobile as searched])
Now there is another example which is:
DB::table('users')
->where('name', '=', 'Rohan')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})->get();
It depicts:
select * from users where name = 'Rohan' or (votes > 100 and title <> 'Admin')
I hope with the help of these you can solve the issue.