->pluck laravel code example

Example 1: firstwhere laravel

Role::firstWhere('name', 'admin')
Role::firstWhere(['name' => 'admin', 'display_name' => 'Administrateur'])

Example 2: pluck array in laravel

$users = User::all()->pluck('field_name');
//for keys instead of [User::all()->pluck('id');] use
$user_ids = User::all()->modelKeys();

Example 3: laravel pluck

$users = Users::pluck('name','email');
dd($users);

Example 4: laravel array_pluck

$array = [
    ['developer' => ['id' => 1, 'name' => 'Taylor']],
    ['developer' => ['id' => 2, 'name' => 'Abigail']],
];

$array = array_pluck($array, 'developer.name');

// ['Taylor', 'Abigail'];

Tags:

Php Example