File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
src/Illuminate/Database/Eloquent/Concerns
tests/Integration/Database Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -1117,6 +1117,18 @@ public function setRelations(array $relations)
1117
1117
return $ this ;
1118
1118
}
1119
1119
1120
+ /**
1121
+ * Enable relationship autoloading for this model.
1122
+ *
1123
+ * @return $this
1124
+ */
1125
+ public function withRelationshipAutoloading ()
1126
+ {
1127
+ $ this ->newCollection ([$ this ])->withRelationshipAutoloading ();
1128
+
1129
+ return $ this ;
1130
+ }
1131
+
1120
1132
/**
1121
1133
* Duplicate the instance and unset all the loaded relations.
1122
1134
*
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ protected function afterRefreshingDatabase()
32
32
});
33
33
}
34
34
35
- public function testRelationAutoload ()
35
+ public function testRelationAutoloadForCollection ()
36
36
{
37
37
$ post1 = Post::create ();
38
38
$ comment1 = $ post1 ->comments ()->create (['parent_id ' => null ]);
@@ -63,6 +63,29 @@ public function testRelationAutoload()
63
63
$ this ->assertTrue ($ posts [0 ]->comments [0 ]->relationLoaded ('likes ' ));
64
64
}
65
65
66
+ public function testRelationAutoloadForSingleModel ()
67
+ {
68
+ $ post = Post::create ();
69
+ $ comment1 = $ post ->comments ()->create (['parent_id ' => null ]);
70
+ $ comment2 = $ post ->comments ()->create (['parent_id ' => $ comment1 ->id ]);
71
+ $ comment2 ->likes ()->create ();
72
+ $ comment2 ->likes ()->create ();
73
+
74
+ DB ::enableQueryLog ();
75
+
76
+ $ likes = [];
77
+
78
+ $ post ->withRelationshipAutoloading ();
79
+
80
+ foreach ($ post ->comments as $ comment ) {
81
+ $ likes = array_merge ($ likes , $ comment ->likes ->all ());
82
+ }
83
+
84
+ $ this ->assertCount (2 , DB ::getQueryLog ());
85
+ $ this ->assertCount (2 , $ likes );
86
+ $ this ->assertTrue ($ post ->comments [0 ]->relationLoaded ('likes ' ));
87
+ }
88
+
66
89
public function testRelationAutoloadVariousNestedMorphRelations ()
67
90
{
68
91
tap (Post::create (), function ($ post ) {
You can’t perform that action at this time.
0 commit comments