Array_unique on a laravel eloquent collection
Asked Answered
C

4

18

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

Create answered 5/9, 2013 at 2:0 Comment(2)
I;m not sure what the content of your db-table is, but is it not easier to get the wanted result from a query? - and, of you want to debug your lines of code, try to print some data between your functions and check for correct output.Alter
Have you instead tried using ->groupby('country') or ->distinct()->get(array('country')) in your query?Marathon
F
2

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);
Formyl answered 5/9, 2013 at 14:51 Comment(0)
C
38

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.

Checkerbloom answered 26/3, 2014 at 12:22 Comment(0)
W
7

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.

Woodsia answered 14/4, 2016 at 5:40 Comment(2)
Thanks 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-uniqueDepopulate
tq so much for the reverse() trick. I have been pulling my hairs for hoursCierracig
F
2

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);
Formyl answered 5/9, 2013 at 14:51 Comment(0)
J
0

You could try the filter method on eloquent collections if that's exactly what you want to do

http://laravel.com/docs/eloquent#collections

Jaredjarek answered 5/9, 2013 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.