tzookb's avatar

access specific id inside collection

I want to access a specific object inside a collection I got, for example:

$users = User::whereIn('id', [4,9, ..., 14]);

so as we know it will return a collection with n items of users.

but now I would like to access a specific one with the id 9, I dont really know it is in the second location so I cant do this

$users[1];

or what I would have desire is to simply do this

$users[9];

but the array inside the collection it's keys are 0,1,2,.... no matter the object id's

any idea how to access a specific idea?

0 likes
3 replies
tzookb's avatar

:))

And I read the regular collection api 100 times!!

kreitje's avatar

You can also do the following to get what you want.

$users = User::whereIn('id', [4,9, ..., 14])->getDictionary();
1 like

Please or to participate in this conversation.