Laravel 5.2 - pluck() method returns array

The pluck method is used to retrieve a list of a single column from a database table. It returns an array of values representing the specified column.

Here is an example of how you can use the pluck method in a Laravel 5.2 application:

<?php

$users = DB::table('users')->pluck('name');

// The resulting array will contain a list of user names:
// ['John', 'Jane', 'Joe']

Watch a course Learn object oriented PHP

The pluck method can also accept a second argument, which specifies the key column to use when building the array. For example:

<?php

$users = DB::table('users')->pluck('name', 'id');

// The resulting array will contain a list of user names, with the ids as keys:
// [1 => 'John', 2 => 'Jane', 3 => 'Joe']

You can then use the pluck method to retrieve a list of values from a relationship. For example:

<?php

$users = App\User::with('posts')->get();

$titles = $users->pluck('posts.title');

// The resulting array will contain a list of post titles for all users:
// ['Post 1', 'Post 2', 'Post 3']