1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】ネストしたCollectionの中から、指定したキーの値の一覧を取り出す(pluckメソッド)

Last updated at Posted at 2021-09-28

ネストしたCollectionの中から、指定したキーの値を取り出してまとめたいシーンがあった。

利用バージョン:Laravel 6.20.32

pluckメソッド

pluck()メソッドは、下記のようなCollectionから、特定のキー(下記の例では'name')の値のみ取り出したいときに使用する。

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$plucked = $collection->pluck('name');

$plucked->all();

// ['Desk', 'Chair']

collapseメソッド

collapseメソッドは、配列のコレクションをフラットな一次コレクションに展開できる。
pluckに加えてこのメソッドも使用する。

$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

$collapsed = $collection->collapse();

$collapsed->all();

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

ネストしたCollectionを用意して、pluck実行

tinkerでCollectionの挙動を確認する。ターミナルでtinkerを起動。

php artisan tinker

例1

ネストしたCollectionを用意する。

$collection_child = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$collection = collect([
    ['package' => $collection_child, 'name' => 'Desk&Chair'],
]);

下記のようなCollectionが得られる。

=> Illuminate\Support\Collection {#4422
     all: [
       [
         "package" => Illuminate\Support\Collection {#4439
           all: [
             [
               "product_id" => "prod-100",
               "name" => "Desk",
             ],
             [
               "product_id" => "prod-200",
               "name" => "Chair",
             ],
           ],
         },
         "name" => "Desk&Chair",
       ],
     ],
   }

実行

下記のように実行することで、ネストしたキーの値を取得できる。

$collection->pluck('package')->collapse()->pluck('name');

// アスタリスクでつないで記述も可能
$collection->pluck('package.*.name')->collapse();

結果

=> Illuminate\Support\Collection {#4528
     all: [
       "Desk",
       "Chair",
     ],
   }

例2

複数ネストしたCollectionを用意する。

$collection_child1 = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$collection_child2 = collect([
    ['product_id' => 'prod-300', 'name' => 'Light'],
    ['product_id' => 'prod-400', 'name' => 'Laptop'],
]);

$collection = collect([
    ['package' => $collection_child1, 'name' => 'Desk&Chair'],
    ['package' => $collection_child2, 'name' => 'Light&Laptop'],
]);

下記のようなCollectionが得られる。

=> Illuminate\Support\Collection {#4462
     all: [
       [
         "package" => Illuminate\Support\Collection {#4529
           all: [
             [
               "product_id" => "prod-100",
               "name" => "Desk",
             ],
             [
               "product_id" => "prod-200",
               "name" => "Chair",
             ],
           ],
         },
         "name" => "Desk&Chair",
       ],
       [
         "package" => Illuminate\Support\Collection {#4414
           all: [
             [
               "product_id" => "prod-300",
               "name" => "Light",
             ],
             [
               "product_id" => "prod-400",
               "name" => "Laptop",
             ],
           ],
         },
         "name" => "Light&Laptop",
       ],
     ],
   }

実行

例1と同様に、下記のように実行することで、ネストしたキーの値を取得できる。

$collection->pluck('package')->collapse()->pluck('name');

// アスタリスクでつないで記述も可能
$collection->pluck('package.*.name')->collapse();

結果

下記のようなCollectionが得られる。

=> Illuminate\Support\Collection {#4510
     all: [
       "Desk",
       "Chair",
       "Light",
       "Laptop",
     ],
   }
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?