how to use or where in laravel code example
Example 1: laravel orWhere
$camps = $field->camps()->where('status', 0)->where(function ($q) {
$q->where('sex', Auth::user()->sex)->orWhere('sex', 0);
})->get();
Example 2: AND-OR-AND + brackets with Eloquent
//mysql query be like this
// ... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65)
//Eloquent query is
// ...
$q->where(function ($query) {
$query->where('gender', 'Male')
->where('age', '>=', 18);
})->orWhere(function($query) {
$query->where('gender', 'Female')
->where('age', '>=', 65);
})
//@sujay
Example 3: or where in orm laravel
$a='foo', $b='bar', $c='john', $d='doe';
Coder::where(function ($query) use ($a, $b) {
$query->where('a', '=', $a)
->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
$query->where('c', '=', $c)
->orWhere('d', '=', $d);
})->get();
Example 4: laravel where
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();