search

Home  >  Q&A  >  body text

MYSQL query where clause using OR and AND in LARAVEL

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

I tried this but I got an error

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

P粉008829791P粉008829791455 days ago507

reply all(1)I'll reply

  • P粉775788723

    P粉7757887232023-09-16 00:23:40

    You must use the 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();

    reply
    0
  • Cancelreply