How to get duplicate values from Laravel Collections
In this tutorial, we will learn How to get duplicate values from Laravel Collection. I think you can understand the concept of Laravel collection default getting duplicates. You will learn Laravel collection in finding duplicates with collection in filter duplicates value. and we will find duplicate value count. So, let’s without west time create an example of getting duplicate values from Laravel collections.
I give you a simple example of how to get duplicate values from the Laravel collection. and you can easily use it with your Laravel 6, Laravel 7, and Laravel 7 application.
If you are looking for all the below question answers?, if yes. then you are right place here.
Laravel Collection Get Duplicates Values Example?
Laravel gets duplicate value from Laravel Collections?
How to get duplicate values from Laravel collections?
Use Laravel Collection to get duplicate values?
Laravel Collection Duplicates Method Example?
How to get duplicate keys Laravel collection?
Finding Duplicate Data in Laravel collections?
Remove Duplicates from Collection Laravel?
Laravel collection removes duplicate value?
how to check duplicate records in Laravel?
Syntax
$collecton->duplicates(Key As Optional);
Laravel Collection duplicates() method
Example
public function index() {
$collection = collect([1991,1992,1993,1994,1995,1996,1997,1997,1996,1994]);
$result = $collection->duplicates();
echo '<pre>';
print_r($result);
}
Output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[7] => 1997
[8] => 1996
[9] => 1994
)
)
Laravel Collection duplicates() with Argument
Example
public function index()
{
$collection = collect([
["id"=>1, "name"=>"Joi", "age"=> 29],
["id"=>2, "name"=>"Die", "age"=> 28],
["id"=>3, "name"=>"Emily", "age"=> 27],
["id"=>4, "name"=>"Kel", "age"=> 28],
["id"=>5, "name"=>"jonh", "age"=> 29],
["id"=>6, "name"=>"thomas", "age"=> 29],
]);
$result = $collection->duplicates('age');
echo '<pre>';
print_r($result);
}
Output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[3] => 28
[4] => 29
[5] => 29
)
)
Laravel Collection duplicates Count
Example
public function index()
{
$collection = collect([
["id"=>1, "name"=>"Joi", "city" => "Rajkot", "age"=> 29],
["id"=>2, "name"=>"Die", "city" => "Morbi", "age"=> 28],
["id"=>3, "name"=>"Emily", "city" => "Rajkot", "age"=> 27],
["id"=>4, "name"=>"Kel", "city" => "Morbi", "age"=> 28],
["id"=>5, "name"=>"jonh", "city" => "Surat", "age"=> 29],
["id"=>6, "name"=>"thomas", "city" => "Ahmedabad", "age"=> 29],
]);
$grouped = $collection->groupBy('city')->map(function ($row) {
return $row->count();
});
echo '<pre>';
print_r($grouped);
}
Output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[Rajkot] => 2
[Morbi] => 2
[Surat] => 1
[Ahmedabad] => 1
)
)