Monthly Archive: April 2020

Remove duplicate after union two collections in Laravel 5

Problem: Having two collections/MySQL query results and combine them using union():


then want to remove all duplicate on that union result.

Solution: using unique() to remove duplicates. You can add a parameter like unique(‘id’) to specifically remove which tag name. Example:

$result1 = $result1->unique()->values()->all();

Remember to assign it to a variable or else it will remain the same.

Close Bitnami banner