Laravel Eloquent Query Using WHERE with OR AND OR?
Make use of Logical Grouping (Laravel 7.x/4.2). For your example, it'd be something like this:
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
Laravel or where
Check out the Logical Grouping section in the docs:
https://laravel.com/docs/master/queries#logical-grouping
It explains how to group conditions in the WHERE clause.
It should be something like:
if(isset($_GET['search']))
{
$query->where(function($query){
$query->where('title', 'like', '%' . $_GET['search'] . '%')
->orWhere('description', 'like', '%' . $_GET['search'] . '%');
});
}
Laravel 5 Eloquent where and or in Clauses
Using advanced wheres:
CabRes::where('m__Id', 46)
->where('t_Id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();
Or, even better, using whereIn()
:
CabRes::where('m__Id', 46)
->where('t_Id', 2)
->whereIn('Cab', $cabIds)
->get();
Laravel Eloquent - (where) and (where or where or where)
You need to group the orWhere calls together. The following should do the trick:
if($search_query != ""){
$clients = $clients->where(function($query) use ($search_query) {
$query->where('name','LIKE','%'.$search_query.'%')
->orWhere('email','LIKE','%'.$search_query.'%')
->orWhere('vat_number','LIKE','%'.$search_query.'%')
->orWhere('contact_name','LIKE','%'.$search_query.'%');
});
}
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');
})
Laravel query builder where and or where
Try to find complex query examples with Laravel Query Builder. And maybe this can help a little:
DB::table('users')
->where('id', 1)
->where('address', 'USA')
->where(function($query) {
$query->where('status', 'active')
->orWhere('status', 'pending');
})->get();
or:
DB::table('users')
->where([
['id', 1],
['address', 'USA'],
])
->where(function($query) {
$query->where('status', 'active')
->orWhere('status', 'pending');
})->get();
laravel query builder or inside where
Your query should look like this:
DB::table('table')
->where(function($q) use ($value, $value2) {
$q->where('column', $value)
->orWhere('column', $value2);
})
->where('column2', 'like', '%'.%value3.'%')
->get();
If you have multiple values, you can put them into a simple array and use whereIn()
:
DB::table('table')
->whereIn('column', $valuesArray)
->where('column2', 'like', '%'.%value3.'%')
->get();
laravel where condition for related eloquent
First, 'where' requires two arguments. where('column', 'value')
. Second, until you use a function dedicated for returning the results, like find(1)
, first()
, get()
, you will always get a builder object.
In this particular case, you need to query the relationship. Something like this should work for you.
User::with('userAttr')->whereHas('userAttr', function($query){
return $query->where('job', 'teacher');
})->get();
For reference:
https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
Problem with Laravel Eloquent Where , orWhere and Find in a same eloquent query
You can group conditions when you pass a closure to where
.
$link = Link::query()
->where(function ($query) {
$query->where('user_id', Auth::id())
->orWhere('user_ip', \request()->ip())
})
->with('clicks')
->findOrFail($id);
Related Topics
How to Fix 'Creating Default Object from Empty Value' Warning in PHP
Forbidden Error When Submitting Simple PHP Form
PHP Optional Parameters - Specify Parameter Value by Name
Php: Can Curl Follow Meta Redirects
Phpmailer Character Encoding Issues
PHP Function with Optional Parameters
How to Ipc Between PHP Clients and a C Daemon Server
Php: How to Check If a Date Is Today, Yesterday or Tomorrow
How to Generate an .Xlsx Using PHP
Is There a Limit When Using PHP Mail Function
Turkish Characters Are Not Displayed Correctly
Laravel 5.3 New Auth::Routes()