Laravel Eloquent: Return Array key as the fields ID
You can simply do
Since you have an Eloquent Collection (a child class of the generic
Collection class) you can use the
$collection->getDictionary() will give you an array of your Category objects keyed by their primary keys.
If you wanted another Collection rather than a native PHP array, you could instead use
$collection->keyBy($property). It looks like your primary key is
$collection->keyBy('category_id'). You can use that method to key by any arbitrary property, including any get mutators you may have written.
getDictionary is unique to the Eloquent
keyBy is available to all Laravel
Collection objects. See the Laravel 4.2 API docs or Laravel 5.0 API docs.
You have a
Database\Eloquent\Collection you can use the method
lists('id') to return an array of the id of each of the models within the collection.
array_combine to map the keys to the models. The result of which will be an array with the id mapped to their corresponding model.