Sunday 07 May 2017 at 10:40
Queries by Key in LaravelBy Eric Antoine Scuccimarra
One of my greatest frustrations with Eloquent collections has been that I needed to loop through the collection and either create a new collection or array if I wanted to have the results in a format where I could access them by a value in the query results. That is to say if I want to be able to access the results by say primary key, I would need to loop through the Collection returned by the query and create a new object with the key as whatever I wanted it to be.
I just learned that there is a much easier way to do this:
This will return the collection with "whatever" as the key, which makes life so much easier and code so much cleaner.