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

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.

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 Extend Collection Object

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:

Read Also:  Get all eloquent query log using Laravel 7/6 example

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:  Laravel Installer Create Web Application install scripts

Summary

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

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.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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