2

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
}
]}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Renato
  • 47
  • 8

2 Answers2

2

Use pluck(attribute):

->whereNotIn('id', $collection->pluck('id'))->get();
DevK
  • 9,597
  • 2
  • 26
  • 48
1

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291