Jim Elliott
Jim Elliott

Reputation: 37

Laravel 5.4 whereNotIn apparently not working

I have logic in a controller that builds an array called $exclude.

Using dd for $exclude I get :

array:4 [▼
  0 => 2
  1 => 3
  2 => 4
  3 => 5
]

which is correct.

I want to exclude those from a result so I have:

$potype = DB::table('potypes')
                ->whereNotIn('id',[$exclude])
                ->get();

but when I run the query those items are included with the exception of the first in the array. So I enabled the query log with

DB::enableQueryLog();

and ran

dd(DB::getQueryLog());

with the result of

array:1 [▼
  0 => array:3 [▼
    "query" => "select * from `potypes` where `id` not in (?)"
    "bindings" => array:4 [▼
      0 => 2
      1 => 3
      2 => 4
      3 => 5
    ]
    "time" => 0.67
  ]
]

The table has 8 records but running the query is returning 7, only ommiting the first of the list:

Collection {#621 ▼
  #items: array:7 [▼

If I use implode

$ex = implode(',',$exclude) and change the query to ->whereNotIn('id',[$ex]) I get the same result - 7 items with just the first in the list being ignored.

Is this an Eloquent bug or me?

Upvotes: 0

Views: 552

Answers (2)

Jim Elliott
Jim Elliott

Reputation: 37

OK it was realtively simple with Mohammed's comment pointing me in the right direction.

As $exclude was an array I changed the get to:

$potype = DB::table('potypes')
            ->whereNotIn('id',$exclude)
            ->get();

and it worked OK!

Upvotes: -2

Mohammad Fanni
Mohammad Fanni

Reputation: 4173

delete [ ] and check it again:

$potype = DB::table('potypes')
                ->whereNotIn('id',$exclude)
                ->get();

Upvotes: 3

Related Questions