Friday, March 3, 2017

Laravel 5: How to group a collection dynamically by one column or another depending on item's column value?

I have a collection of items as a result of an Eloquent model query. What I need to do is to somehow group the items of that collection either by one column or another depending on a specific item's field value if it satisfies a condition. Is it possible to use a function inside the groupBy() or there is another way to do this, if possible at all? I'll provide additional details if needed in case there is a theoretical solution for such situation.



via Eseth

Advertisement