Skip to content
Advertisement

Laravel Nested Foreach loop from collection

I have the following collection:

Screenshot: enter image description here What I would like to do is return each state college across in 3 rows by state using a blade template. For example:

Massachusetts

college 1 college 2 college 3

Florida

college 1 college 2 college 3

college 4

I have tried many different suggested examples from other posts, but have not been able to find a solution.

Here is what I have tried:

JavaScript

Any help would be greatly appreciated.

Greg

Advertisement

Answer

From the looks of your collection screenshot, it has the following structure:

JavaScript

Therefore, if you foreach over the $states collection, taking the $key as the state name and the $value as that state’s college collection, you can do the following:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement