Skip to content

All topics  /  Laravel

Laravel Arr pluck() function Example

Laravel ·

Hi Guys,

In this blog,I will you how to use laravel array pluck() function example. We will show example of array pluck function in laravel.The Arr::pluck method retrieves all of the values for a given key from an array.

Here, I will give you full example for simply Arr pluck() method in laravel as bellow.

Example


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\FileController;
use Illuminate\Support\Arr;
class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $array1 = [
            ['developer' => ['id' => 1, 'name' => 'Mehul']],
            ['developer' => ['id' => 2, 'name' => 'Dharmik']],
        ];
        $names1 = Arr::pluck($array, 'developer.name');
        // ['Mehul', 'Dharmik']
        $array2 = [
            ['product' => ['id' => 1, 'name' => 'Pen', 'price' => '100']],
            ['product' => ['id' => 2, 'name' => 'Book', 'price' => '200']],
        ];
        $names2 = Arr::pluck($array, 'product.price');
        // ['100', '200']
    }
}

Output

"['Mehul', 'Dharmik']"
"['100', '200']"

It Will help you...