I’m not sure the title of this topic is correct, I apologize, you suppose I have this list which I get them from the database:
menu-index menu-create menu-store menu-edit menu-update menu-destroy commonCategory-index commonCategory-create commonCategory-store commonCategory-edit commonCategory-update commonCategory-destroy usersRole-index usersRole-create usersRole-store usersRole-edit usersRole-update usersRole-destroy
as you can see we can grouping them with the first part of each item that we can have that them in three groups, after grouping them I want to use foreach
for each one item that’s there. I try to use the Laravel
collection, but I can’t. Could you help me please how can I do that?
Advertisement
Answer
You could do the following:
Model::all() ->groupBy(function($item) { // Might want to add some checks here, but that depends on your setup return explode('-', $item->getAttribute('columnName'))[0]; }) ->each(function($item) { // do sth. });
But it might be more performant to add a group
column to your model already.