Home > Enterprise >  using WHERE condition1 AND condition2 in ::selectraw
using WHERE condition1 AND condition2 in ::selectraw

Time:02-02

I am using ::selectRaw inside my PHP function and I need that statement to get two conditions. In my case, I am using my category_id column

I tried to do two where but seems like these conflict with each other:

    $query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
                   ->where('category_id', '=', 1)
                   ->where('category_id', '=', 2)
                   ->where(function($q) use($search){
        if($search){
            return $q->where(['group_name' => $search]);
        }
    });

how do I translate WHERE category_id = 1 AND category_id = 2 in the ::selectRaw query?

CodePudding user response:

You can use the whereIn function.

$query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
                   ->whereIn('category_id', [1,2])
                   ->where(function($q) use($search){
        if($search){
            return $q->where(['group_name' => $search]);
        }
    });
  •  Tags:  
  • Related