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) 

 [ ![Laravel Overlapping Scheduled Tasks: The Production Problem Nobody Talks About](https://cdn.msaied.com/93/01KTTJBMWPGG4V0TG5B5B6GF9P.png) 

### Laravel Overlapping Scheduled Tasks: The Production Problem Nobody Talks About

Laravel scheduled tasks can silently overlap in production, causing duplicate jobs, race conditions, and faile...

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

 11 Jun 2026     18 min read  

  Read    

 ](https://msaied.com/articles/laravel-overlapping-scheduled-tasks-the-production-problem-nobody-talks-about) [ ![Provision Laravel Cloud From the Stripe CLI](https://cdn.msaied.com/89/7691d1d607cc9d4cb22156215eead147.png) Laravel Stripe CLI Laravel Cloud 

### Provision Laravel Cloud From the Stripe CLI

Streamline your Laravel Cloud provisioning with the Stripe CLI. Spin up infrastructure, manage credentials, an...

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

 10 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/provision-laravel-cloud-from-the-stripe-cli) [ ![JSON Schema Deserialization in Laravel 13.14](https://cdn.msaied.com/87/ec9f2bc8c8c8ba6afb67a065a5e19943.png) Laravel PHP JSON Schema 

### JSON Schema Deserialization in Laravel 13.14

Laravel 13.14.0 introduces a new JSON Schema deserializer, enhancing type object reconstruction from arrays. T...

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

 9 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/json-schema-deserialization-in-laravel-1314) 

   [  ![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)
