Laravel: Get Array of IDs with pluck() or modelKeys() | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Get Array of IDs from Eloquent Collection: pluck() or modelKeys()        On this page       1. [  Using pluck() ](#using-codepluckcode)
2. [  Introducing modelKeys() ](#introducing-codemodelkeyscode)
3. [  When to Use Which? ](#when-to-use-which)
4. [  Key Takeaways: ](#key-takeaways)

  ![Get Array of IDs from Eloquent Collection: pluck() or modelKeys()](https://cdn.msaied.com/96/d7f17428bf750e57ce14033f6a5aa249.png)

 [  Laravel ](https://msaied.com/articles?category=laravel) [  Tips &amp; Tricks ](https://msaied.com/articles?category=tips-tricks)  #laravel   #eloquent   #php   #collection   #pluck   #modelKeys  

 Get Array of IDs from Eloquent Collection: pluck() or modelKeys() 
===================================================================

     24 Aug 2022      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Using pluck()  ](#using-codepluckcode)
2. [  02   Introducing modelKeys()  ](#introducing-codemodelkeyscode)
3. [  03   When to Use Which?  ](#when-to-use-which)
4. [  04   Key Takeaways:  ](#key-takeaways)

 When working with Laravel Eloquent, a common task is to retrieve a collection of records and then extract just their primary keys into an array. This is particularly useful when dealing with relationships, such as fetching all permission IDs associated with a specific role.

Laravel provides elegant solutions for this, with two primary methods standing out: `pluck()` and `modelKeys()`.

Using `pluck()`
---------------

The `pluck()` method is a widely recognized and versatile tool within Laravel collections. It allows you to retrieve a list of values for a specified key from each item in the collection. For instance, if you have a `Role` model that `hasMany` `Permission` models, you can easily get an array of permission IDs like this:

```php
// Assuming $role is an instance of your Role model
$permissionIDs = $role->permissions->pluck('id');

```

This code snippet iterates through the `permissions` collection associated with the `$role` and extracts the value of the `id` key from each permission model, returning them as a simple PHP array.

The `pluck()` method is also available directly on the Eloquent Query Builder, allowing you to fetch only the IDs from the database without retrieving the entire model objects:

```php
$allPermissionIDs = Permission::pluck('id');

```

This approach is highly efficient as it only selects the `id` column from the database.

Introducing `modelKeys()`
-------------------------

Laravel also offers a more specialized method called `modelKeys()`. This method is specifically designed to retrieve an array of the primary keys from an Eloquent Collection. Its official description is concise: "Get the array of primary keys."

Here's how you would use it in the same scenario:

```php
// Assuming $role is an instance of your Role model
$permissionIDs = $role->permissions->modelKeys();

```

Interestingly, `modelKeys()` often results in the same character count as `pluck('id')`, making it a neat alternative. A key distinction is that `modelKeys()` is a method of the `Illuminate\Database\Eloquent\Collection` class, not the Eloquent model itself.

This means you cannot directly call `modelKeys()` on a Query Builder instance:

```php
// This will result in an error:
// "Call to undefined method App\Models\Permission::modelKeys()"
$allPermissions = Permission::modelKeys();

```

To use `modelKeys()` when you need all primary keys from a table, you must first retrieve all the models into a collection:

```php
$allPermissions = Permission::all()->modelKeys();

```

However, be mindful of potential performance implications if you are fetching all records from a very large table. In such cases, using `pluck('id')` directly on the Query Builder is generally more performant.

When to Use Which?
------------------

- **`pluck()`**: Use this method when you need to extract values for a specific key (not necessarily the primary key) from a collection, or when you want to fetch only primary keys directly from the database query builder for efficiency.
- **`modelKeys()`**: Use this method when you already have an Eloquent Collection and want to specifically retrieve an array of its primary keys. It's a clear and semantic way to express your intent.

Both methods are valuable tools in a Laravel developer's arsenal, offering efficient ways to manipulate and extract data from Eloquent collections.

### Key Takeaways:

- `pluck('id')` is versatile and can be used on Query Builders and Collections to extract values by key.
- `modelKeys()` is specifically for Eloquent Collections and retrieves an array of primary keys.
- For fetching all primary keys from a large table, `Permission::pluck('id')` is generally more performant than `Permission::all()->modelKeys()`.

Source: [Get Array of IDs from Eloquent Collection: pluck() or modelKeys()](https://laraveldaily.com/post/eloquent-get-array-of-ids-from-collection-pluck-or-modelkeys)

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fget-array-of-ids-from-eloquent-collection-pluck-or-modelkeys&text=Get+Array+of+IDs+from+Eloquent+Collection%3A+pluck%28%29+or+modelKeys%28%29) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fget-array-of-ids-from-eloquent-collection-pluck-or-modelkeys) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  What is the difference between `pluck()` and `modelKeys()` in Laravel?        `pluck()` is a more general method that can extract values for any specified key from a collection or directly from a query builder. `modelKeys()` is specifically designed for Eloquent Collections and retrieves an array of their primary keys. 

      Q02  Can I use `modelKeys()` directly on an Eloquent Query Builder?        No, `modelKeys()` is a method of the `Illuminate\Database\Eloquent\Collection` class. You cannot call it directly on a Query Builder. You must first fetch the results into a collection using methods like `all()` or by accessing a relationship. 

      Q03  Which method is more performant for getting all IDs from a large table?        For fetching all primary keys from a large table, using `YourModel::pluck('id')` directly on the Query Builder is generally more performant than retrieving all models with `YourModel::all()` and then calling `modelKeys()`. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Concurrency in Laravel: Process Pools, Fibers, and the Concurrency Facade](https://cdn.msaied.com/471/ba1a628c196e92cbb81033c865c9ad26.png) laravel concurrency php 

### Concurrency in Laravel: Process Pools, Fibers, and the Concurrency Facade

Laravel's Concurrency facade and process pools let you run independent work in parallel without reaching for a...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 26 Jul 2026     4 min read  

  Read    

 ](https://msaied.com/articles/concurrency-in-laravel-process-pools-fibers-and-the-concurrency-facade) [ ![Laravel Queues in Production: Horizon Tuning, Metrics, and Graceful Scaling](https://cdn.msaied.com/470/afd2bee56d15842d72c1c6d5c238d236.png) laravel horizon queues 

### Laravel Queues in Production: Horizon Tuning, Metrics, and Graceful Scaling

Go beyond basic queue setup. Learn how to tune Horizon supervisor processes, interpret queue metrics, handle b...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 26 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/laravel-queues-in-production-horizon-tuning-metrics-and-graceful-scaling) [ ![Laravel Horizon: Queue Metrics, Supervisor Tuning, and Graceful Scaling in Production](https://cdn.msaied.com/469/ebbc461b808da425a418bf6ffc998d7a.png) laravel horizon queues 

### Laravel Horizon: Queue Metrics, Supervisor Tuning, and Graceful Scaling in Production

Beyond the dashboard: how to tune Horizon supervisors, interpret queue metrics, and scale workers gracefully w...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 25 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/laravel-horizon-queue-metrics-supervisor-tuning-and-graceful-scaling-in-production-1) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
