Laravel Collection Unique | Remove Duplicates from Collection Laravel
Hi Artisan,
In this tutorial, you will learn laravel collection remove duplicates. This article goes in detailed on laravel remove duplicates from database. you can see how to remove duplicate rows in laravel collection. if you want to see example of remove duplicates from collection laravel then you are a right place. Let's get started with how to remove duplicate rows in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
In this example i will explain you how to use laravel collection uniuqe and i will give you some example that will help you to delete duplicates records from collection in laravel.
Let's see bellow one by one example with output:
Example 1:
public function index()
{
$myCollection = collect([1, 2, 2, 3, 4, 4, 5, 6, 4, 2]);
$uniqueCollection = $myCollection->unique();
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => 1
[1] => 2
[3] => 3
[4] => 4
[6] => 5
[7] => 6
)
)
Example 2:
public function index()
{
$myCollection = collect([
['id'=>1, 'name'=>'Hardik', 'state'=>'Gujarat', 'country'=>'India'],
['id'=>2, 'name'=>'Paresh', 'state'=>'Gujarat', 'country'=>'India'],
['id'=>3, 'name'=>'Vimal', 'state'=>'MP', 'country'=>'India'],
['id'=>4, 'name'=>'John', 'state'=>'New York', 'country'=>'US'],
['id'=>5, 'name'=>'Ken', 'state'=>'New York', 'country'=>'US'],
]);
$uniqueCollection = $myCollection->unique('country');
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[state] => Gujarat
[country] => India
)
[3] => Array
(
[id] => 4
[name] => John
[state] => New York
[country] => US
)
)
)
Example 3:
public function index()
{
$myCollection = collect([
['id'=>1, 'name'=>'Hardik', 'state'=>'Gujarat', 'country'=>'India'],
['id'=>2, 'name'=>'Paresh', 'state'=>'Gujarat', 'country'=>'India'],
['id'=>3, 'name'=>'Vimal', 'state'=>'MP', 'country'=>'India'],
['id'=>4, 'name'=>'John', 'state'=>'New York', 'country'=>'US'],
['id'=>5, 'name'=>'Ken', 'state'=>'New York', 'country'=>'US'],
]);
$uniqueCollection = $myCollection->unique(function ($item) {
return $item['country'].$item['state'];
});
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[state] => Gujarat
[country] => India
)
[2] => Array
(
[id] => 3
[name] => Vimal
[state] => MP
[country] => India
)
[3] => Array
(
[id] => 4
[name] => John
[state] => New York
[country] => US
)
)
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Collection Search Method Example
- Laravel Collection Filter Method Example
- Laravel 8/7 Paginate with Collection or Array
- Laravel Order By Relationship Sum Column Example
- Laravel Relationship Eager Loading with Condition Example
- Merge Multiple Collection Paginate in Laravel
- Laravel Relationship Where Condition Example
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel Has Many Through Eloquent Relationship Tutorial