8
1

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.

【Laravel8】collectionを扱うためのメソッドを試してみる①(all、avg、chunk、collapse、combine)

Last updated at Posted at 2021-06-02

Laravelにはcollectionを扱うためのメソッドがたくさん用意されています!
たくさんあるので試してみました。

間違い等あればご指摘お願い致します!

参考:https://readouble.com/laravel/8.x/ja/collections.html
とりあえずメソッド一覧を順番に使ってみようかと思います。
全てTinkerで試しています。

all( )

collectionの裏の配列表現を返します。

//collectionを作成
>>> $weather = collect(['sunny','cloudy','rainy'])

//allメソッド
>>> $weather->all();
=> [
     "sunny",
     "cloudy",
     "rainy",
   ]

モデルの値全て取得するときなどは以下のように使いますね。

User::all();

avg( )

平均値を返します。

>>> $weight = collect([30,40,50,60])->avg();
=> 45

chunk( )

指定したサイズで複数の小さなcollectionにします。
主にビューで使うみたいですね。

//collection作成
>>> $animals = collect(['dog','cat','rabbit','cow','wolf','fox','mouse','horse'])
=> Illuminate\Support\Collection {#4294
     all: [
       "dog",
       "cat",
       "rabbit",
       "cow",
       "wolf",
       "fox",
       "mouse",
       "horse",
     ],
   }

//chunkメソッド
>>> $pair = $animals->chunk(2);
=> Illuminate\Support\Collection {#4313
     all: [
       Illuminate\Support\Collection {#4295
         all: [
           "dog",
           "cat",
         ],
       },
       Illuminate\Support\Collection {#4293
         all: [
           2 => "rabbit",
           3 => "cow",
         ],
       },
       Illuminate\Support\Collection {#4303
         all: [
           4 => "wolf",
           5 => "fox",
         ],
       },
       Illuminate\Support\Collection {#4314
         all: [
           6 => "mouse",
           7 => "horse",
         ],
       },
     ],
   }

おお、2つずつに別れましたね。

collapse( )

配列のコレクションを一次コレクションにします。

//collectionを作成
$numbers = collect([[10,20,30],[100,200,300],[1000,2000,3000]]);
=> Illuminate\Support\Collection {#4322
     all: [
       [
         10,
         20,
         30,
       ],
       [
         100,
         200,
         300,
       ],
       [
         1000,
         2000,
         3000,
       ],
     ],
   }

//collapse()メソッド
$collapsed = $numbers->collapse();
=> Illuminate\Support\Collection {#4301
     all: [
       10,
       20,
       30,
       100,
       200,
       300,
       1000,
       2000,
       3000,
     ],
   }

一次のcollecttionになりました!

combine( )

コレクションの値をキーとして他の配列かコレクションの値を結合します。

//collection作成
$collection = collect(['name', 'hobby']);
=> Illuminate\Support\Collection {#4318
     all: [
       "name",
       "hobby",
     ],
   }

//combine()メソッド
>>> $combined = $collection->combine(['aya','music']);
=> Illuminate\Support\Collection {#4305
     all: [
       "name" => "aya",
       "hobby" => "music",
     ],
   }

//以下のように2つ値を与えてみても1つしかダメっぽい。
>>> $combined = $collection->combine(['aya','music'],['nozomi','anime']);
=> Illuminate\Support\Collection {#4296
     all: [
       "name" => "aya",
       "hobby" => "music",
     ],
   }

んーこれはどういう時に使うんだろう。。。

6/3 追記
使う場面について

chunk と combine は CSV ファイルから DB にデータをインポートする時に便利ですね

と教えて頂きました!
ありがとうございます!

今回はここまでにします!

8
1
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
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?