Array_unique on a laravel eloquent collection

Array_unique on a laravel eloquent collection

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Array_unique on a Laravel Eloquent Collection

You cannot pass a Illuminate\Support\Collection instance directly to PHP's native array_unique() function. The error you are seeing, "Array to string conversion," occurs because array_unique() expects a plain PHP array containing scalar values (strings, integers, or floats). When it encounters a Collection object, it tries to cast that object into a string using its default magic method behavior, which fails unless the collection specifically implements that logic for debugging purposes.

The root cause in your controller logic is the line where you extract the countries:

$jobs = Job::search();
$countries = $jobs->get()->map(function( $job ) {
    return $job->country;
});
$countries = array_unique( $countries->toArray() );

The map method returns a new Collection instance, even if the mapped values are simple strings. When you call array_unique() directly on $countries, PHP throws the error. To fix this immediately, you must convert the collection to an array using the toArray() method before passing it to array_unique().

$countries = $jobs->get()->map(function($job) {
    return $job->country;
})->unique(); // Preferred approach in Laravel

However, relying on native PHP functions inside Eloquent chains often misses the point of using an ORM. Laravel provides a unique() method on collections that handles this scenario more elegantly and efficiently. By calling $collection->unique(), you avoid the overhead of converting the entire collection to a raw array in memory just to filter duplicates. This keeps your code within the Eloquent ecosystem, allowing for better lazy loading behavior if your collection is massive.

If you strictly need to use array_unique()—perhaps because you are performing a specific operation that requires a plain array later—you should ensure the conversion happens before the function call:

$countries = $jobs->get()
    ->map(function($job) {
        return $job->country;
    })
    ->toArray(); // Convert to array first

$countries = array_unique($countries);

There is a significant trade-off here regarding data types. The native array_unique() function compares values by value and removes duplicates based on strict type checking in some contexts, but its behavior can be unpredictable if your data contains mixed types that PHP treats as equal but represent differently. For example, it might treat 1 and "1" as distinct depending on the version of PHP and the specific implementation details. In contrast, Laravel's collection unique() method allows you to specify a key and strict comparison logic:

// Remove duplicates based on the 'country' property if it were an object list
$countries = $jobs->get()->unique('country'); 

If your goal is simply to get a list of unique country names, mapping first creates an intermediate array which might be unnecessary. A more direct approach using pluck and then unique() reduces memory usage:

$uniqueCountries = Job::search()
    ->get()
    ->pluck('country') // Extract the 'country' column into an array of strings
    ->unique();        // Remove duplicates while keeping it as a Collection

This approach is generally preferred because pluck is optimized for accessing specific model attributes, and chaining unique() keeps the data in a collection structure until you actually need to iterate or export it. This prevents accidental serialization issues that can arise when working with raw arrays in larger applications. Always remember that mixing native array functions with Eloquent collections without explicit type conversion is the primary source of these runtime errors.

Our network