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)
11171117 return $ this ;
11181118 }
11191119
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+
11201132 /**
11211133 * Duplicate the instance and unset all the loaded relations.
11221134 *
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ protected function afterRefreshingDatabase()
3232 });
3333 }
3434
35- public function testRelationAutoload ()
35+ public function testRelationAutoloadForCollection ()
3636 {
3737 $ post1 = Post::create ();
3838 $ comment1 = $ post1 ->comments ()->create (['parent_id ' => null ]);
@@ -63,6 +63,29 @@ public function testRelationAutoload()
6363 $ this ->assertTrue ($ posts [0 ]->comments [0 ]->relationLoaded ('likes ' ));
6464 }
6565
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+
6689 public function testRelationAutoloadVariousNestedMorphRelations ()
6790 {
6891 tap (Post::create (), function ($ post ) {
You can’t perform that action at this time.
0 commit comments