Collectionでのmap()
とtransform()
のメソッドは似ているようで大きな違いがあります。
まず、テストデータのレコードをtinkerで3つ作成します。
>>> User::factory(3)->create(); => Illuminate\Database\Eloquent\Collection {#4463 all: [ App\Models\User {#4464 id: 1, name: "坂本 知実", email: "tfujimoto@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "mBZjRiwtp2", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, App\Models\User {#4465 id: 2, name: "高橋 知実", email: "kazuya.miyazawa@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "2ERHaySLEo", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, App\Models\User {#4466 id: 3, name: "藤本 修平", email: "mkiriyama@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "JpPxv9yr0d", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, ], }
作成されたレコードを$users
の変数に入れます。
>>> $users = User::all();
map()
もtransform()
もCollectionの中のそれぞれのアイテムを処理してCollectionの値を返すのは同じです。
まず、mapから、
>>> $users->map(function ($user) { return $user->only(['id', 'name']); }) => Illuminate\Support\Collection {#4473 all: [ [ "id" => 1, "name" => "坂本 知実", ], [ "id" => 2, "name" => "高橋 知実", ], [ "id" => 3, "name" => "藤本 修平", ], ], }
この後に$users
の中身を見てみましょう。
>>> $users => Illuminate\Database\Eloquent\Collection {#4463 all: [ App\Models\User {#4464 id: 1, name: "坂本 知実", email: "tfujimoto@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "mBZjRiwtp2", birth_date: "1988-03-20", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, App\Models\User {#4465 id: 2, name: "高橋 知実", email: "kazuya.miyazawa@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "2ERHaySLEo", birth_date: "1975-06-25", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, App\Models\User {#4466 id: 3, name: "藤本 修平", email: "mkiriyama@example.net", email_verified_at: "2022-04-03 02:36:03", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", #remember_token: "JpPxv9yr0d", birth_date: "2011-06-17", created_at: "2022-04-03 02:36:03", updated_at: "2022-04-03 02:36:03", }, ], }
何も変わっていませんね。
今度は、transformです。
>>> $users->transform(function($user) { return $user->only(['id', 'name']); }) => Illuminate\Database\Eloquent\Collection {#4463 all: [ [ "id" => 1, "name" => "坂本 知実", ], [ "id" => 2, "name" => "高橋 知実", ], [ "id" => 3, "name" => "藤本 修平", ], ], }
ここまではmapの結果とまったく同じですね。
しかし、もとの変数の中身を見ると、
>>> $users => Illuminate\Database\Eloquent\Collection {#4463 all: [ [ "id" => 1, "name" => "坂本 知実", ], [ "id" => 2, "name" => "高橋 知実", ], [ "id" => 3, "name" => "藤本 修平", ], ], }
中身が返り値と同じ値で上書きとなっているのです。transformという名前はそれゆえにです。
メルマガ購読の申し込みはこちらから。