【Laravel5.7】Eloquent Collectionの使い方(load)
こんにちは。
ニシザワです。
Eloquent Collectionの使い方の個別記事です。
まとめ記事は こちら
2.引数がarrayの場合は、リレーション先を早期ロードしなおします。
ニシザワです。
Eloquent Collectionの使い方の個別記事です。
まとめ記事は こちら
load(array|string $relation) : Collection
Laravelのソースと解説
/**
* Load a set of relationships onto the collection.
*
* @param array|string $relations
* @return $this
*/
public function load($relations)
{
if ($this->isNotEmpty()) {
if (is_string($relations)) {
$relations = func_get_args();
}
$query = $this->first()->newQueryWithoutRelationships()->with($relations);
$this->items = $query->eagerLoadRelations($this->items);
}
return $this;
}
1.引数がstringの場合は、引数をarrayにして、リレーション先を早期ロードしなおします。2.引数がarrayの場合は、リレーション先を早期ロードしなおします。
loadの使い方
1.引数がstringの場合
$models = Collection::make([
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
App\User {
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
]);
$models = $models->load("posts", "posts.comments");
/*
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2018-10-31 11:09:34",
updated_at: "2018-10-31 11:09:34",
posts: Illuminate\Database\Eloquent\Collection {#3060
all: [
App\Post {
id: 2,
content: "test",
user_id: 3,
created_at: "2019-02-04 11:38:34",
updated_at: "2019-02-04 11:38:34",
comments: Illuminate\Database\Eloquent\Collection {#3065
all: [
App\Comment {#3069
id: 3,
comment: "comment1",
user_id: 3,
post_id: 2,
created_at: "2019-02-04 11:59:07",
updated_at: "2019-02-04 11:59:07",
},
App\Comment {#3070
id: 4,
comment: "comment2",
user_id: 3,
post_id: 2,
created_at: "2019-02-04 11:59:11",
updated_at: "2019-02-04 11:59:11",
},
],
},
},
],
},
},
App\User {#3040
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
posts: Illuminate\Database\Eloquent\Collection {#3038
all: [],
},
},
早期ロードしたいリレーションがとれます。
*/
2.引数がarrayの場合
$models = Collection::make([
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
App\User {
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
]);
$models = $models->load(["posts", "posts.comments"]);
/*
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2018-10-31 11:09:34",
updated_at: "2018-10-31 11:09:34",
posts: Illuminate\Database\Eloquent\Collection {#3060
all: [
App\Post {
id: 2,
content: "test",
user_id: 3,
created_at: "2019-02-04 11:38:34",
updated_at: "2019-02-04 11:38:34",
comments: Illuminate\Database\Eloquent\Collection {#3065
all: [
App\Comment {#3069
id: 3,
comment: "comment1",
user_id: 3,
post_id: 2,
created_at: "2019-02-04 11:59:07",
updated_at: "2019-02-04 11:59:07",
},
App\Comment {#3070
id: 4,
comment: "comment2",
user_id: 3,
post_id: 2,
created_at: "2019-02-04 11:59:11",
updated_at: "2019-02-04 11:59:11",
},
],
},
},
],
},
},
App\User {#3040
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
posts: Illuminate\Database\Eloquent\Collection {#3038
all: [],
},
},
早期ロードしたいリレーションがとれます。
*/
ちょっとした裏仕様
$models = Collection::make([
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
App\User {
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
},
]);
$models = $models->load(["posts:user_id,id", "posts.comments:post_id,comment"]);
/*
App\User {
id: 1,
name: "test",
email: "hoge@fuga.com",
email_verified_at: null,
created_at: "2018-10-31 11:09:34",
updated_at: "2018-10-31 11:09:34",
posts: Illuminate\Database\Eloquent\Collection {#3020
all: [
App\Post {#3063
user_id: 3,
id: 2,
comments: Illuminate\Database\Eloquent\Collection {#3074
all: [
App\Comment {#3079
post_id: 2,
comment: "comment1",
},
App\Comment {#3080
post_id: 2,
comment: "comment2",
},
],
},
},
],
},
},
App\User {#3040
id: 2,
name: "test2",
email: "hoge2@fuga.com",
email_verified_at: null,
created_at: "2019-02-04 11:37:47",
updated_at: "2019-02-04 11:37:47",
posts: Illuminate\Database\Eloquent\Collection {#3038
all: [],
},
},
:(コロン)で区切ると必要なプロパティのみ取得できます。
ただ、リレーション関係にあるプロパティを含まないと先のリレーションが取れません。
例えば
$models->load("posts:id");
この場合、user_idが外部キーなので、取得プロパティに含まれないと取得されないので注意です。
*/
本日はここまでとします。
ありがとうございました。