Laravel How To Get Array of Ids From Eloquent Model

Reading Time: 3 minutes
1,061 Views

Inside this article we will see the concept i.e How To Get Array of Ids From Eloquent Model in Laravel. Article contains the classified information about Getting only ids from array of objects in laravel Code.

Sometimes when you work, you need to get an array of ids from the table. So, this tutorial will be helpful for you for this. Tutorial is super easy to understand and implement it in your code as well.

There are two ways to get an array of ids in laravel. Laravel provides pluck() and modelKeys() eloquent methods to get an array from the collection.

This article is not for any laravel version specific. You can use this concept with Laravel v6, Laravel v7, Laravel v8 also.

Read More: How To Capture Website Screenshot with Laravel Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Method #1: Using pluck()

Here, we will use pluck() method of eloquent model to get an array of IDs,

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $usersIDs = User::pluck('id')->toArray();
  
        dd($usersIDs);
    }
}

Read More: Convert HTML to PDF Using Javascript Example Tutorial

Concept

$usersIDs = User::pluck('id')->toArray();

Output

Method #2: Using modelKeys()

Here, we will use modelKeys() method of eloquent model to get an array of IDs,

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $usersIDs = User::all()->modelKeys();
  
        dd($usersIDs);
    }
}

Concept

$usersIDs = User::all()->modelKeys();

Output

We hope this article helped you to learn Laravel How To Get Array of Ids From Eloquent Model Tutorial in a very detailed way.

Read More: Laravel Move Data From One Table To Another Table Example

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.