HDTuto.com

Laravel 7 Eloquent Pluck to Array Example



This post will give you example of laravel 7 pluck method example. we will help you to give example of laravel 7 pluck query builder. if you want to see example of laravel db query pluck then you are a right place. This post will give you simple example of laravel 7 pluck to array.

Here, i will show you when we need to use pluck() in laravel. 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...




May 13, 2020 | Category : Laravel



Blog Search