ItSolutionStuff.com

Laravel Pluck Method Example | Laravel Pluck()

By Hardik Savani • April 16, 2024
Laravel

This is a short guide on laravel pluck example. you will learn laravel pluck method example. it's simple example of laravel pluck query builder. you can understand a concept of laravel db query pluck.

Here, i will show you when we need to use pluck() in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11. When ever you need to get all ids with name in array for your select dropdown from database in laravel at that time we have to use pluck() of collection.

Here, i will give you two example of laravel eloquent get all ids in array using collection pluck().

so, let's see bellow example:

Example 1:

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$ids = Users::pluck('id');

dd($ids);

}

Output:

array:[

0 => 1

1 => 2

2 => 3

]

Example 2:

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$names = Users::pluck('name', 'id');

dd($names);

}

Output:

array:4 [

1 => "Hardik"

2 => "Paresh"

3 => "Rajesh"

]

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Collection Implode Method Example

Read Now →

Laravel Collection Map Method Example

Read Now →

Laravel Collection SortBy Tutorial with Examples

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel Collection Unique | Remove Duplicates from Collection Laravel

Read Now →

Laravel Collection Search Method Example

Read Now →

Laravel Relationship Where Condition Example

Read Now →

Laravel Eloquent Relationships Tutorial From Scratch

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel One to Many Polymorphic Relationship Tutorial

Read Now →