سؤال

How would I take something with a crazy N+1 issue like:

        foreach ($serie->categories as $category) {
            foreach ($category->processes as $process) {
                foreach ($process->cards as $card) {
                    foreach ($card->groups as $group) {
                        $group->upload;
                    }
                }
            }
        }

And make it into one or two statements? I'm using Laravel's Eloquent ORM.

هل كانت مفيدة؟

المحلول

You can do deeply nested eager loading with:

Serie::with('categories.processes.cards.groups')->get();

This already loads all processes per category, cards per process, etc.

Look at the documentation

$books = Book::with('author.contacts')->get();

If you want a quick way to get all groups, try:

$series = Serie::with('categories.processes.cards.groups')->get()->toArray();

$groups = array_pluck($series, 'categories.processes.cards.groups');

This returns the attributes for every group in array format. You only need to find a new way to perform the ->upload() method on the group object.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top