Skip to content

Combine two DB query results in Laravel?

We can merge easily laravel’s query result with another result object, by using laravel’s Collections method called merge(), It will merge a collection with another collection, and return a single collection containing all the elements in the two objects.


// get all film projects
$filmProjects= FilmProject::all();

// get all new media projects
$newMediaProjects=  NewMediaProject::all();

// merge projects,
foreach($filmProjects as $filmProject) {
    $newMediaProjects->add($filmProject);
}

// or we can also do this $newMediaProjects->toBase()->merge($filmProjects);

dd($newMediaProjects->toArray());



That’s it.

5 1 vote
Article Rating
Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments