laravel pluck with array code example

Example 1: collection laravel filter

$collection = collect([1, 2, 3, 4]);

$filtered = $collection->filter(function ($value, $key) {
    return $value > 2;
});

$filtered->all();

// [3, 4]

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: pluck laravel

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

Example 4: laravel pluck

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

Tags:

Php Example