Array_unique on a laravel eloquent collection
Solution 1
You can have unique values in your DB results using distinct
or group by
in your select clause. But, if you really need to have unique values over an array of object you can do the following:
$uniques = array();
foreach ($countries as $c) {
$uniques[$c->code] = $c; // Get unique country by code.
}
dd($uniques);
Solution 2
You could try the Unique method of the Collection class:
$countries = $countries->unique();
The Collection class has several wonderful methods. You could read about this in the Laravel API documentation.
I agree that sometimes it is more efficient to "query" on an existing Collection in memory (in stead of doing another query on the database using the Querybuilder class), like for example you first want to count and then filter. In .NET LINQ you can query almost equally on an IEnumerable (in-memory collection) as on a database, something I really enjoy.
Solution 3
I had similar issue and although time have passed since it may be useful to someone today.
The Problem I had was that when I called unique
method on collection of items it didn't worked, that is probably the reason the first answer got accepted. So if you have models and you want to remove duplicates based on a specific field you can pass parameter to your unique
method, in this case it would be:
$countries->unique('code');
that way you'll only have countries with unique codes. You may notice that only the first value stays, so if you develop a shopping cart application and want for some reason merge carts and only want to have recent items you can just reverse the collection and call unique
and reverse it back:
$countries->reverse()->unique('code')->reverse(); // it doesn't really make sense in this example though
it is probably not the best option and it is better to do filtering on the database side but it is nice to have options.
Related videos on Youtube
AndrewMcLagan
I’m a full stack Web Developer with over fifteen years experience in the industry. I concentrate on open source languages and technologies such as PHP 5.6+, Python, Javascript EMCA6, AngularJS, React, Laravel and Symfony. My strengths would be a very strong interest in the open source community and approaching webapp development from a software design perspective rather than simply being a codemonkey. Strong understanding of basic SOLID principals and the patterns that make these principles a reality. Coming from many years experience as a contract developer, keeping up-to-date with the open source community, tools and best practices has been paramount to my career. Also, I LOVE to code!
Updated on July 09, 2022Comments
-
AndrewMcLagan almost 2 years
Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working.
my controller logic:
// init models $jobs = Job::search(); $countries = $jobs->get()->map(function( $job ) { return $job->country; }); $countries = array_unique( $countries->toArray() );
although this gets a "Array to string conversion" error
-
kjones about 5 yearsThanks for this; I missed this in the docs but it is indeed described and an example is provided: laravel.com/docs/5.3/collections#method-unique
-
Afif Zafri over 4 yearstq so much for the reverse() trick. I have been pulling my hairs for hours