Answer the question
In order to leave comments, you need to log in
How to get an array from a model with keys by id in Eloquent?
I have a many-to-many relationship between the User and Role models,
I get the user's roles
$user = User::where('id', $id);
$user->roles->toArray();
Accordingly, the role keys go in order from 0 and beyond
Is it possible somehow without writing foreach etc, using regular eloquent tools, to make the array keys correspond to the id of a specific role
That is, instead
of [0] = id => 4, role = ROLE_USER
[1 ] = id => 7, role = ROLE_ADMIN
Was
[4] = id => 4, role = ROLE_USER
[7] = id => 7, role = ROLE_ADMIN
or even so
[4] = ROLE_USER
[7] = ROLE_ADMIN
Answer the question
In order to leave comments, you need to log in
Collections have many great methods for conveniently working with data.
There is a 99.99% chance that you need one of them, and not make keys in the form of IDs.
https://laravel.com/docs/5.6/eloquent-collections#...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question