unique collection method can be used to get all the unique items in the collection. it’s part of the Enumerable contract.
unique collection
also you can safely use it to get a Collection instance. and then you can read my prev article like as Get Unique Collection Values(Remove Duplicates From Collection) Using Laravel
Example 1: Laravel Controller Code:
unique(); dd($records); } }
Result
Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 3 [9] => 6 [10] => 8 [11] => 9 )
Don’t miss : Laravel Sum Query with Where Condition Example
Example 2: Controller Code:
1, "name" => "Ridham"], [ "id" => 1, "name" => "Ridham"], [ "id" => 2, "name" => "Mayur"], [ "id" => 3, "name" => "Kishan"], ], [ [ "id" => 1, "name" => "Ridham"], [ "id" => 3, "name" => "Keyur"], ] ] ); $records = $records->map(function ($array) { return collect($array)->unique('id')->all(); }); dd($records); } }
Result
Array ( [0] => Array ( [0] => Array ( [id] => 1 [name] => Ridham ) [2] => Array ( [id] => 2 [name] => Mayur ) [3] => Array ( [id] => 3 [name] => Kishan ) ) [1] => Array ( [0] => Array ( [id] => 1 [name] => Ridham ) [1] => Array ( [id] => 3 [name] => Keyur ) ) )
I hope you get an idea about unique collection.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.