Laravel Collection Unique | Remove Duplicates from Collection Laravel

By Hardik Savani | April 14, 2020 | Category : 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.

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
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: