Jazz
Jazz

Reputation: 175

where column = NULL and whereNotIn not working together in laravel

My code not working together, but if i only use one of them->where('numbers.client_group_id', NULL) OR ->whereNotIn('id', function($query) use ($request){ itu work normal.

$model = DB::table('numbers')->select('id','phone');

    $model
      ->where('numbers.client_group_id', NULL)
      ->whereNotIn('id', function($query) use ($request){
        $query->select('number_push_logs.number_id')->from('number_push_logs')->where('number_push_logs.client_group_id',$request->client_group);
      })
      ->orderBy('quality', 'asc')->limit($request->total_push);

this my table looks like:

numbers table

| id| phone      | client_group_id|
|---|------------|----------------|
| 1 |0852176255**| 1              |
| 2 |0852176255**| 1              |
| 3 |0852176255**| NULL           |

number_push_logs table

| id| number_id  |client_group_id |
|---|------------|----------------|
| 1 |1           | 1              |
| 2 |1           | 2              |
| 3 |2           | 2              |

so i want to select numbers data where client_group_id = NULL and id numbers not in number_push_log table with specific client_group_id

Upvotes: 0

Views: 200

Answers (1)

Martin Osusky
Martin Osusky

Reputation: 845

Try something like this. Use join. It performs better than nested demand.

       \DB::table('numbers as n')
            ->select('id','phone') 
            ->leftJoin('number_push_logs as npl', 'n.client_group_id', '=', 'npl.client_group_id')
            ->whereNull('npl.id')
            ->orderBy('quality', 'asc')
            ->limit($request->total_push);

Upvotes: 2

Related Questions