Search code examples
laravellaravel-5.3laravel-5.4

How to easy extract data from nested collections?


I use many-to-many relationships in Laravel.

In result I get collection with nested relations like this:

User->categories->announcements

How to get easy last nested collection announcements?


Solution

  • Just use nested foreach loops. For example, if user is just one here (and not a collection of users):

    @foreach ($user->categories as $category)
        @foreach ($category->announcements as $announcement)
            {{ $announcement->title }}
        @endforeach
    @endforeach