How to Remove Duplicate Rows In Laravel Collection ?
Teams applying “How to Remove Duplicate Rows In Laravel Collection ?” in a production project can use the official product overview to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi Guys,
In this example,I will learn you how to remove duplicates from collection laravel.you can easy and simply remove duplicates from collection laravel.
The unique method returns all of the unique items in the collection. The returned collection keeps the original array keys and value.
Let's see bellow one by one example with output:
Example 1:
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
$myCollection = collect(['a','b','b','c','d','e']);
$uniqueCollection = $myCollection->unique();
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection {#276 ?
#items: array:5 [?
0 => "a"
1 => "b"
3 => "c"
4 => "d"
5 => "e"
]
}
Example 2:
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
$myCollection = collect([
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);
$uniqueCollection = $myCollection->unique('type');
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection {#276 ?
#items: array:2 [?
0 => array:3 [?
"name" => "iPhone 6"
"brand" => "Apple"
"type" => "phone"
]
2 => array:3 [?
"name" => "Apple Watch"
"brand" => "Apple"
"type" => "watch"
]
]
}
Example 3:
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
$myCollection = collect([
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);
$uniqueCollection = $myCollection->unique(function ($item) {
return $item['brand'].$item['type'];
});
$uniqueCollection->all();
dd($uniqueCollection);
}
Output:
Illuminate\Support\Collection {#276 ?
#items: array:4 [?
0 => array:3 [?
"name" => "iPhone 6"
"brand" => "Apple"
"type" => "phone"
]
2 => array:3 [?
"name" => "Apple Watch"
"brand" => "Apple"
"type" => "watch"
]
3 => array:3 [?
"name" => "Galaxy S6"
"brand" => "Samsung"
"type" => "phone"
]
4 => array:3 [?
"name" => "Galaxy Gear"
"brand" => "Samsung"
"type" => "watch"
]
]
}
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial