Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array_unique on a laravel eloquent collection

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

like image 518
AndrewMcLagan Avatar asked Sep 05 '13 02:09

AndrewMcLagan


People also ask

What is Array_unique?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.

What does toArray do in Laravel?

Laravel 5 Collections: Convert a Collection to a Native PHP Array With toArray. The toArray method is similar to the all method in that it will return the underlying array that the collection instance is using.

IS NOT NULL in Laravel eloquent?

Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .

How do you make an array of objects unique in PHP?

array_unique works by casting the elements to a string and doing a comparison. Unless your objects uniquely cast to strings, then they won't work with array_unique. Instead, implement a stateful comparison function for your objects and use array_filter to throw out things the function has already seen.


2 Answers

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.

like image 71
Jorr.it Avatar answered Nov 02 '22 20:11

Jorr.it


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.

like image 20
orustammanapov Avatar answered Nov 02 '22 21:11

orustammanapov