where and where 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: eloquent where in

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get()

Example 4: laravel with and where

$projects = Project::whereHas('projectOffers', function ($offer) {
            $offer->where('teacher_id', "Ahmed");
            $offer->where('status_id', "Accepted");
        })->where('status_id', "inprogress")->get();

Example 5: laravel where

$users = DB::table('users')
                ->whereMonth('created_at', '12')
                ->get();

Example 6: 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();

Tags:

Php Example