Laravel Collection Unique | Remove Duplicates from Collection Laravel

Laravel Collection Unique | Remove Duplicates from Collection Laravel

In this post we will give you information about Laravel Collection Unique | Remove Duplicates from Collection Laravel. Hear we will give you detail about Laravel Collection Unique | Remove Duplicates from Collection LaravelAnd how to use it also give you demo for it if it is necessary.

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:

IlluminateSupportCollection 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:

IlluminateSupportCollection 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:

IlluminateSupportCollection 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…

Hope this code and post will helped you for implement Laravel Collection Unique | Remove Duplicates from Collection Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  2  =  

We accept paid advance and paid guest Posting on our Site : Contact US