Laravel: Get Array of IDs with pluck() or modelKeys() | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. [Laravel](https://www.msaied.com/public/articles?category=laravel)
6. /
7. Get Array of IDs from Eloquent Collection: pluck() or modelKeys()

   [Laravel](https://www.msaied.com/public/articles?category=laravel) [Tips &amp; Tricks](https://www.msaied.com/public/articles?category=tips-tricks) 

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

 Discover two efficient methods, `pluck()` and `modelKeys()`, to extract arrays of IDs from Laravel Eloquent collections. Learn when to use each for optimal performance and code clarity.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 24 Aug 2022 · Updated 11 Jun 2026 · 3 min read

ShareCopy linkCopied

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

  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)

 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)

- [laravel](https://www.msaied.com/public/articles?search=laravel)
- [eloquent](https://www.msaied.com/public/articles?search=eloquent)
- [php](https://www.msaied.com/public/articles?search=php)
- [collection](https://www.msaied.com/public/articles?search=collection)
- [pluck](https://www.msaied.com/public/articles?search=pluck)
- [modelKeys](https://www.msaied.com/public/articles?search=modelKeys)

 Frequently asked questions 
---------------------------

  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.

   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.

   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()`.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

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

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Next articleLeveraging Carbon Constants for Cleaner Date and Time Logic in Laravel](https://www.msaied.com/public/articles/leveraging-carbon-constants-for-cleaner-date-and-time-logic-in-laravel)  

   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)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
