Get Unique Collection values(Remove Duplicates from Collection) using Laravel

Rate this post

Today, We want to share with you Get Unique Collection values(Remove Duplicates from Collection) using Laravel.In this post we will show you Get unique array values with Laravel 5/6/7 Collections, hear for Laravel Collection Helper Methods (also Eloquent) : unique we will give you demo and example for implement.In this post, we will learn about laravel query builder remove duplicates with an example.

Free Live Chat for Any Issue

Get Unique Collection values(Remove Duplicates from Collection) using Laravel

There are the Following The simple About laravel collection unique multiple columns Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel collection->unique by key, so the some major files and Directory structures for this example is following below.

Read Also:  Laravel Eloquent with Collection Contains

When dealing with nested any data of arrays or objects, We can specify the key used to all the determine uniqueness – in this example only first of all the matching collection items will be included in output data collection.

laravel eloquent remove duplicates

The unique() method returns all of the unique items in the collection.

Example 1:

public function index()
{
    $teamCollectionPlayerList = collect([1, 2, 2, 3, 4, 4, 5, 6, 4, 2]);
    $teamPlayerEligableCollect = $teamCollectionPlayerList->unique();
    $teamPlayerEligableCollect->all();
    dd($teamPlayerEligableCollect);
}

Results:
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => 1
            [1] => 2
            [3] => 3
            [4] => 4
            [6] => 5
            [7] => 6
        )
)

laravel collection remove duplicates

Example 2:

The returned collection keeps the original array keys.

public function index()
{
  
    $teamCollectionPlayerList = collect([
                    ['id'=>1, 'name'=>'Balaji', 'state'=>'Rajkot', 'country'=>'Pakistan'],
                    ['id'=>2, 'name'=>'Paresh', 'state'=>'Rajkot', 'country'=>'Pakistan'],
                    ['id'=>3, 'name'=>'Mansukh', 'state'=>'Gondal', 'country'=>'Pakistan'],
                    ['id'=>4, 'name'=>'Angat', 'state'=>'Kalavad', 'country'=>'UK'],
                    ['id'=>5, 'name'=>'Ken', 'state'=>'Kalavad', 'country'=>'UK'],
                ]);
      
    $teamPlayerEligableCollect = $teamCollectionPlayerList->unique('country');
      
    $teamPlayerEligableCollect->all();
      
    dd($teamPlayerEligableCollect);
}

Results:
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Balaji
                    [state] => Rajkot
                    [country] => Pakistan
                )
            [3] => Array
                (
                    [id] => 4
                    [name] => Angat
                    [state] => Kalavad
                    [country] => UK
                )
        )
)

laravel remove duplicates from database

Example 3:

public function index()
{
  
    $teamCollectionPlayerList = collect([
                    ['id'=>1, 'name'=>'Balaji', 'state'=>'Rajkot', 'country'=>'Pakistan'],
                    ['id'=>2, 'name'=>'Paresh', 'state'=>'Rajkot', 'country'=>'Pakistan'],
                    ['id'=>3, 'name'=>'Mansukh', 'state'=>'Gondal', 'country'=>'Pakistan'],
                    ['id'=>4, 'name'=>'Angat', 'state'=>'Kalavad', 'country'=>'UK'],
                    ['id'=>5, 'name'=>'Ken', 'state'=>'Kalavad', 'country'=>'UK'],
                ]);
      
    $teamPlayerEligableCollect = $teamCollectionPlayerList->unique(function ($item) {
                            return $item['country'].$item['state'];
                        });
       
    $teamPlayerEligableCollect->all();
      
    dd($teamPlayerEligableCollect);
}

Results
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Balaji
                    [state] => Rajkot
                    [country] => Pakistan
                )
            [2] => Array
                (
                    [id] => 3
                    [name] => Mansukh
                    [state] => Gondal
                    [country] => Pakistan
                )
            [3] => Array
                (
                    [id] => 4
                    [name] => Angat
                    [state] => Kalavad
                    [country] => UK
                )
        )
)

Web Programming Tutorials Example with Demo

Read :

Read Also:  PHP Laravel Full Text Search in MYSQL Database

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Download

I hope you get an idea about Laravel Collection Unique.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.