A
A
Alexey Verkhovtsev2018-04-17 11:37:12
Laravel
Alexey Verkhovtsev, 2018-04-17 11:37:12

How to merge laravel collections into one?

Hello. There are sites, each site has bugs, you need to get 1 total collection, so that later you can do pagination. I do this and always get an empty collection.

foreach ($sites as $site) {
            //dd();
            $collection->merge($site->bugs()->where('employee_id', $userId)->get());
        }

And here it works
$bugs1 = $sites[0]->bugs()->where('employee_id', $userId)->get();
        $bugs2 = $sites[1]->bugs()->where('employee_id', $userId)->get();
        $all = $collection->merge($bugs1)->merge($bugs2);

but I need dynamics, there may be 100500 sites

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-04-17
@seftomsk

$collection = $collection->merge(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question