How to return Eloquent models' single column values as an array
In Laravel, selecting a specific field often feels counterintuitive when you expect raw data back. Consider a scenario where you have a Foo model linked to the foos table with columns id, a, b, and c. Your database contains rows like (1, 10, 15, 20) or (1, 12, 15, 27). If you need to retrieve all values of column a where column b equals 15, the standard approach is:
Foo::select('a')->where('b', 15)->get();
This returns an Eloquent Collection. While this is useful for further manipulation within Laravel's ORM ecosystem, it introduces overhead when you simply need a flat array of values like [10, 12, 17]. The core issue isn't the query itself, but how Eloquent hydrates that data into model instances by default.
To convert the result into a plain array, you must explicitly tell Eloquent to transform the collection. The most direct method involves calling the toArray() method on the collection returned by get().
$values = Foo::select('a')->where('b', 15)->get()->toArray();
This converts the collection into a standard PHP array where each element represents a row. However, this is not yet what you want. The resulting array will look like this:
[
['a' => 10],
['a' => 12],
['a' => 17],
]
Each item is still an associative array representing a row object, not a simple scalar value. To extract just the column values into a flat list, you need to iterate over the collection and map the specific key. Using pluck is the idiomatic solution for this exact pattern:
$values = Foo::select('a')->where('b', 15)->pluck('a');
The pluck() method returns an Eloquent Collection containing only the values of the specified column, discarding the keys. To get a plain array, chain all() onto it:
$values = Foo::select('a')->where('b', 15)->pluck('a')->all();
// Result: [10, 12, 17]
Alternatively, you can use the toArray() method directly after plucking, though all() is generally preferred for converting collections to arrays in this context. Both approaches yield the same result:
$values = Foo::select('a')->where('b', 15)->pluck('a')->toArray();
Performance Considerations
When working with large datasets, it is important to understand what happens under the hood. The select('a') clause ensures that only the necessary column is fetched from the database, minimizing network transfer and memory usage compared to selecting all columns (*). This is a critical optimization when dealing with high-cardinality tables.
However, even with column selection, Eloquent still hydrates each row into an object instance before pluck() extracts the scalar values. For massive result sets, this hydration can become a bottleneck. In such cases, switching to raw queries via DB::select() or using Laravel's query builder without the model might offer better performance:
$values = DB::table('foos')
->select('a')
->where('b', 15)
->pluck('a');
This approach bypasses the Eloquent model entirely, returning a raw collection that still requires all() or toArray() to become a plain array. The trade-off is losing access to model features like fillable attributes or event listeners, which might be necessary if you later need to manipulate the data within Laravel's context.
When to Use Each Approach
If your goal is to pass these values directly into a view or a third-party script that expects a simple array, pluck('a')->all() is the cleanest solution. It keeps the code readable while delivering exactly what is needed. If you need to access related data or model methods later, keeping the Eloquent collection returned by get() allows you to iterate over full models.
For most scenarios involving single-column filtering, sticking with the model's query builder and chaining pluck() maintains consistency with Laravel's conventions without sacrificing performance significantly.
