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://www.msaied.com) [ Home ](https://www.msaied.com) [ Projects ](https://www.msaied.com/projects) [ Articles  ](https://www.msaied.com/articles) [ Certificates ](https://www.msaied.com/certificates) [ Contact ](https://www.msaied.com#contact-section) 

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

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

  [ home ](https://www.msaied.com)    [ articles ](https://www.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://www.msaied.com/articles?category=laravel) [  Tips &amp; Tricks ](https://www.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%2Fwww.msaied.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%2Fwww.msaied.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://www.msaied.com/articles) 

 [ ![MySQL Query Profiling in Laravel: EXPLAIN ANALYZE, Slow Query Log, and Index Hints](https://cdn.msaied.com/481/8bfc417cb94b3e2868428e065fe4b166.png) laravel mysql performance 

### MySQL Query Profiling in Laravel: EXPLAIN ANALYZE, Slow Query Log, and Index Hints

Stop guessing why a query is slow. Learn how to use EXPLAIN ANALYZE, Laravel's slow query log listener, and in...

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

 28 Jul 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/mysql-query-profiling-in-laravel-explain-analyze-slow-query-log-and-index-hints) [ ![Laravel Custom Eloquent Relations: Building Polymorphic and Composite-Key Relations](https://cdn.msaied.com/480/c7d2c0d0fd1823460fbdb138f77b573f.png) laravel eloquent database 

### Laravel Custom Eloquent Relations: Building Polymorphic and Composite-Key Relations

Eloquent's built-in relations cover 90% of cases, but composite-key joins and non-standard polymorphic pivots...

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

 28 Jul 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/laravel-custom-eloquent-relations-building-polymorphic-and-composite-key-relations) [ ![PostgreSQL JSONB in Laravel: Indexing, Querying, and Casting Without the Overhead](https://cdn.msaied.com/479/d7c54bd7a42ee57610a29f19a2c5e0fa.png) laravel postgresql jsonb 

### PostgreSQL JSONB in Laravel: Indexing, Querying, and Casting Without the Overhead

JSONB columns unlock flexible schemas, but misused they become performance sinkholes. This guide covers GIN in...

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

 28 Jul 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/postgresql-jsonb-in-laravel-indexing-querying-and-casting-without-the-overhead) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.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://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [Contact](https://www.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)
