Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: whereNotIn not working as expect

I have an array with values 1,2. for example.

$arr = []

foreach($permission as $perm)
{
  $arr[] = $perm->permission
}

dd(implode(',',$arr));
result shows 1,2

but when i use the $arr in query its not working proper. I am using this in following query

  $response = implode(',',$arr);
  $role = Role::whereNotIn('id',[$response])->get(); 
  //means select * from role where id not in (1,2);

but it works like below

 select * from role where id not in (1);

can you guys please help me to resolve this

like image 972
Muhammad Kazim Avatar asked Oct 24 '25 04:10

Muhammad Kazim


1 Answers

You're passing a string which is "1,2". You must pass an array not a string to the whereNotIn() method:

$role = Role::whereNotIn('id', $arr)->get();
like image 162
Alexey Mezenin Avatar answered Oct 26 '25 20:10

Alexey Mezenin