1

I have a mysql query in which I need make a check for array of values in 2 columns using Or. Inshort I have one array which needs to be checked on 2 columns using Or condition.

using sql

SELECT *
FROM table_name
WHERE col_1 IN (1, 8, 3) 
OR
WHERE col_2 IN (1, 8, 3) 

using laravel

        $mu = DB::table('users')
        ->join('table_a', 'users.id', '=', 'table_a.user_id')
        ->select('table_a.user_id')
        //This are all AND
        ->where('table_b.search_status','available')
        ->where('table_b.role','<>',2)

        //This 2 parts needs to be in OR clause
        ->whereIn('table_b.lang',$seeks)
        ->whereIn('table_b.lang_2',$seeks)

        //This is AND too
        ->whereIn('table_a.language_id',$lang)
        ->get()->pluck('user_id')->toArray();

I want to do something like:

         ->where(function($q) 
        {
            $q->whereIn('table_b.lang',$seeks)
              ->orWhere(whereIn('table_b.lang_2',$seeks));
        })

But gives error: undefined $seeks.

2 Answers 2

1

Use a closure where condition to wrap them.

$mu = DB::table('users')
    ->join('table_a', 'users.id', '=', 'table_a.user_id')
    ->select('table_a.user_id')
    ->where('table_b.search_status', 'available')
    ->where('table_b.role','<>', 2)
    ->where(function ($query) use ($seeks) {
        $query->whereIn('table_b.lang', $seeks)
            ->orWhereIn('table_b.lang_2', $seeks);
    })
    ->whereIn('table_a.language_id', $lang)
    ->get()->pluck('user_id')->toArray();
Sign up to request clarification or add additional context in comments.

Comments

0

Just replace this:-

  ->where(function($q) use ($seeks)
    {
        $q->whereIn('table_b.lang',$seeks)
          ->orWhere(whereIn('table_b.lang_2',$seeks));
    });

Into

 ->where(function($q) use ($seeks)
    {
        $q->whereIn('table_b.lang',$seeks)
          ->orWhere(whereIn('table_b.lang_2',$seeks));
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.