Home > Net >  MYSQL query for Where clause with OR & AND in LARAVEL
MYSQL query for Where clause with OR & AND in LARAVEL

Time:12-24

I want to select subcategories where categories_id is 31 or 211 or 18 and where status is on and mode is also on.

I tried this but I error

    $subcategories =DB::table('subcategories')
    ->where('categories_id','31')
    ->orWhere('categories_id','211')
    ->orWhere('categories_id','18')
    ->where('status','on')
    ->where('mode','on')
    ->get();

CodePudding user response:

You have to use function:

$subcategories =DB::table('subcategories')
->where(function($query)
    {
        $query->where('categories_id', '31')
              ->orWhere('categories_id', '211')
              ->orWhere('categories_id', '18');
    })
->where('status','on')
->where('mode','on')
->get();
  • Related