LoginSignup
107
105

More than 3 years have passed since last update.

Laravelコレクションのpluck()メソッドを活用しよう

Last updated at Posted at 2019-07-16

環境

Laravel Framework 5.8
PHP 7.2

やりたいこと

DBからこのようなデータを取得して、idがキー、カラムデータがバリューとなる、配列を作りたくなることがよくある。

        $collection = collect([
            ['id' => 1, 'name' => 'ジャック鈴木', 'company' => 'Five Needs'],
            ['id' => 2, 'name' => 'Umichiko' , 'company' => 'Five Needs']
        ]);

私はこれまで、このようなときarray_column()を利用して配列を生成していた。

      $user_array = array_column($collection->all(),null,"id");

スマートなやり方

Laravelコレクションの機能であるpluck()メソッドを利用することで同様のことがより簡潔に実現できる。

        $collection = collect([
            ['id' => 1, 'name' => 'ジャック鈴木', 'company' => 'Five Needs'],
            ['id' => 2, 'name' => 'Umichiko' , 'company' => 'Five Needs']
        ]);

        /*
         * nameの値だけを取得できる。
         */
        var_dump($collection->pluck("name"));

        //class Illuminate\Support\Collection#331 (1) {
        //  protected $items =>
        //  array(2) {
        //    [0] =>
        //    string(18) "ジャック鈴木"
        //    [1] =>
        //    string(8) "Umichiko"
        //  }
        //}


        /*
         * キーがidでバリューがnameのコレクションを生成
         */
        var_dump($collection->pluck("name", "id"));

        //class Illuminate\Support\Collection#331 (1) {
        //  protected $items =>
        //  array(2) {
        //    [1] =>
        //    string(18) "ジャック鈴木"
        //    [2] =>
        //    string(8) "Umichiko"
        //  }
        //}



        /*
         * キーがidでバリューに全データが入ったコレクションを生成
         */
        var_dump($collection->pluck(null, "id"));

        //class Illuminate\Support\Collection#331 (1) {
        //  protected $items =>
        //  array(2) {
        //    [1] =>
        //    array(3) {
        //      'id' =>
        //      int(1)
        //      'name' =>
        //      string(18) "ジャック鈴木"
        //      'company' =>
        //      string(10) "Five Needs"
        //    }
        //    [2] =>
        //    array(3) {
        //      'id' =>
        //      int(2)
        //      'name' =>
        //      string(8) "Umichiko"
        //      'company' =>
        //      string(10) "Five Needs"
        //    }
        //  }
        //}

        /*
         * array_columnでも同様のことができる
         */
        var_dump(array_column($collection->all(),null,"id"));
        //array(2) {
        //  [1] =>
        //  array(3) {
        //    'id' =>
        //    int(1)
        //    'name' =>
        //    string(18) "ジャック鈴木"
        //    'company' =>
        //    string(10) "Five Needs"
        //  }
        //  [2] =>
        //  array(3) {
        //    'id' =>
        //    int(2)
        //    'name' =>
        //    string(8) "Umichiko"
        //    'company' =>
        //    string(10) "Five Needs"
        //  }
        //}

107
105
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
107
105