Transform collective to array in Laravel, Laravel 5

358 views Asked by At

How can I transform this collection to array and eject "whereNotIn" using a Laravel query, like this:

->whereNotIn('id', ['collection'])->get();'


Collection {#259 ▼
#items: array:3 [▼
0 => {#257 ▼
  +"id": 2
}
1 => {#256 ▼
  +"id": 3
}
2 => {#237 ▼
  +"id": 6
}
]}
2

There are 2 answers

0
Marcin Nabiałek On BEST ANSWER

In fact, to get an array, you should use pluck together with the all() method, so in this case you should use:

->whereNotIn('id', $collection->pluck('id')->all())->get();
0
DevK On

Use pluck(attribute):

->whereNotIn('id', $collection->pluck('id'))->get();