If you need id
as the key, then rely on the Collection:
$collection = Model::all();
$collection->getDictionary();
// returns:
array(
1 => object(Model) ( ... ),
2 => object(Model) ( ... ),
...
idN => object(Model) ( ... )
);
Otherwise, nice or not, you can do this:
$keys = $collection->lists('columnName');
$arrayOfModels = array_combine($keys, $collection->getDictionary);
I'm not sure the code below was there when this thread was opened, but there's a clean way to do it now:
$collection = Model::all();
$rekeyedCollection = $collection->keyBy('some_attribute');
And in my use case I wanted a unique set of Model
s so I then used:
$set = $rekeyedCollection = $rekeyedCollection->unique();
Hope that helps :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community