Eloquent ORM laravel 5 Get Array of ids

Eloquent ORM laravel 5 Get Array of ids

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting ID Arrays from Eloquent Queries in Laravel 5

When working with the Eloquent ORM in Laravel 5, a common pattern emerges when you need to manipulate collections of records based on their primary keys. You might have a model like Test and run a query to fetch all records where the ID is greater than zero. Using the standard syntax provided by Eloquent, this looks something like:

$test = Test::select('id')
    ->where('id', '>', 0)
    ->get()
    ->toArray();

This approach works perfectly fine if you need to inspect the full model instances or access other attributes later. However, it returns an array of arrays, where each inner array contains the id key:

Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 2 ) )

In many scenarios, particularly when passing data to JavaScript or performing further calculations, this nested structure is unnecessary. You simply want a flat array of integers:

Array ( 1, 2 )

Achieving this in Laravel 5 requires leveraging the pluck method, which was introduced specifically to solve this exact problem. The get() method returns a collection of model instances or arrays, while pluck() extracts a specific column from each record and returns a simple key-value array or a single flat array depending on how it is chained.

To transform your nested result into a clean list of IDs, you can modify the chain to use pluck instead of toArray. The syntax shifts slightly because pluck operates on the collection before it is converted to a plain PHP array. You would execute:

$testIds = Test::where('id', '>', 0)
    ->pluck('id')
    ->all();

Here, pluck('id') creates a collection where each item's key is the ID itself and the value is also the ID (effectively flattening the data structure). The subsequent call to .all() converts that collection into a simple PHP array. If you omit the .all() call, you will still have a Laravel Collection object, which is useful if you plan to use its methods again.

There is an important distinction to keep in mind regarding performance and memory usage. While pluck is efficient for single column extraction, be cautious when combining it with other operations that require full model hydration. If you need the IDs but also want to ensure the query only selects the necessary columns (as in your original example with select('id')), you can combine both:

$testIds = Test::select('id')
    ->where('id', '>', 0)
    ->pluck('id')
    ->all();

This ensures that the database query is optimized to retrieve only the id column, preventing unnecessary data transfer. The trade-off here is that you lose access to any model events (like retrieved) or mutators if you are not hydrating the full model, but since you are explicitly selecting only the ID, this is usually the intended behavior.

One potential pitfall in Laravel 5 is understanding how pluck handles duplicate keys. Since your condition filters for unique IDs greater than zero, duplicates are unlikely unless your data integrity is compromised. However, if you were plucking a non-unique column like an email address, later entries would overwrite earlier ones in the resulting array. Always ensure your query logic aligns with the uniqueness requirements of the columns you extract.

If you find yourself needing to pass these IDs directly into JavaScript without converting them to a string array first, pluck()->all() provides the most seamless integration. For more complex scenarios involving multiple tables or eager loading, remember that pluck operates on the result set after all joins and relationships have been resolved, so performance impacts are generally minimal compared to fetching full models.

Our network