Laravel 6 Pluck Example | How to use pluck() in Laravel 6

Laravel 6 , Laravel

Nicesnippets

473

08-01-2020


Hi Guys,

In this tutorial,i will explain how you can use pluck method in laravel 6. we are show simple example of pluck query builder example in laravel 6. you can table to get array in values using laravel pluck method.

it can get value column and key using pluck method.pluck method to returned collection single column or specify a custom key column following example.

here the use pluck example.

Example 1

here example single column for the returned Collection

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$ids = \DB::table('posts')->pluck('id');

dd($ids);

}

Output

array:[

0 => 1

1 => 2

2 => 3

]

Example 2

here example specify a custom key column for the returned Collection

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$users = User::pluck('name','id');

dd($users);

}

Output

array:4 [

1 => "abc"

2 => "xyz"

3 => "mno"

]

it will help you...


Recommended Posts