To retrieve an array of IDs from a collection in Laravel, you can use the pluck
method with the id
attribute:
1 2 3 | $ids = $collection->pluck('id')->all(); |
This will retrieve the id
attribute for each item in the collection, and return an array containing all of the values.
Alternatively, you can use the map
method to transform the items in the collection and return an array of the transformed values:
1 2 3 4 5 | $ids = $collection->map(function ($item) { return $item->id; })->all(); |
Both of these approaches will return an array of IDs that can be used for further processing or manipulation.