3
3
namespace Illuminate \Tests \Integration \Database \EloquentModelRelationAutoloadTest ;
4
4
5
5
use DB ;
6
+ use Illuminate \Database \Eloquent \Factories \Factory ;
7
+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
6
8
use Illuminate \Database \Eloquent \Model ;
7
9
use Illuminate \Database \Schema \Blueprint ;
8
10
use Illuminate \Support \Facades \Schema ;
@@ -12,6 +14,14 @@ class EloquentModelRelationAutoloadTest extends DatabaseTestCase
12
14
{
13
15
protected function afterRefreshingDatabase ()
14
16
{
17
+ Schema::create ('tags ' , function (Blueprint $ table ) {
18
+ $ table ->increments ('id ' );
19
+ $ table ->string ('name ' )->nullable ();
20
+ $ table ->string ('status ' )->nullable ();
21
+ $ table ->unsignedInteger ('post_id ' )->nullable ();
22
+ $ table ->unsignedInteger ('video_id ' )->nullable ();
23
+ });
24
+
15
25
Schema::create ('posts ' , function (Blueprint $ table ) {
16
26
$ table ->increments ('id ' );
17
27
});
@@ -214,6 +224,95 @@ public function testRelationAutoloadVariousNestedMorphRelations()
214
224
215
225
DB ::disableQueryLog ();
216
226
}
227
+
228
+ public function testDoesntCauseNQueryIssueUsingGet ()
229
+ {
230
+ Model::automaticallyEagerLoadRelationships ();
231
+
232
+ DB ::enableQueryLog ();
233
+
234
+ $ post = Post::create ();
235
+
236
+ $ video = Video::create ();
237
+ $ video2 = Video::create ();
238
+ $ video3 = Video::create ();
239
+
240
+ Tag::create (['post_id ' => $ post ->id , 'video_id ' => $ video ->id ]);
241
+ Tag::create (['post_id ' => $ post ->id , 'video_id ' => $ video2 ->id ]);
242
+ Tag::create (['post_id ' => $ post ->id , 'video_id ' => $ video3 ->id ]);
243
+
244
+ $ videos = [];
245
+ foreach ($ post ->tags ()->get () as $ tag ) {
246
+ $ videos [] = $ tag ->video ;
247
+ }
248
+
249
+ $ this ->assertCount (12 , DB ::getQueryLog ());
250
+ $ this ->assertCount (3 , $ videos );
251
+
252
+ Model::automaticallyEagerLoadRelationships (false );
253
+ }
254
+
255
+ public function testRelationAutoloadWorksOnCreatingEvent ()
256
+ {
257
+ Model::automaticallyEagerLoadRelationships ();
258
+
259
+ DB ::enableQueryLog ();
260
+
261
+ $ tags = Tag::factory ()->times (3 )->make ();
262
+
263
+ $ post = Post::factory ()->create ();
264
+
265
+ $ post ->tags ()->saveMany ($ tags );
266
+
267
+ $ this ->assertCount (7 , DB ::getQueryLog ());
268
+
269
+ Model::automaticallyEagerLoadRelationships (false );
270
+
271
+ DB ::disableQueryLog ();
272
+ }
273
+ }
274
+
275
+ class TagFactory extends Factory
276
+ {
277
+ protected $ model = Tag::class;
278
+
279
+ public function definition ()
280
+ {
281
+ return [];
282
+ }
283
+ }
284
+
285
+ class Tag extends Model
286
+ {
287
+ use HasFactory;
288
+
289
+ public $ timestamps = false ;
290
+
291
+ protected $ guarded = [];
292
+
293
+ protected static function booted ()
294
+ {
295
+ static ::creating (function ($ model ) {
296
+ if ($ model ->post ->shouldApplyStatus ()) {
297
+ $ model ->status = 'Todo ' ;
298
+ }
299
+ });
300
+ }
301
+
302
+ protected static function newFactory ()
303
+ {
304
+ return TagFactory::new ();
305
+ }
306
+
307
+ public function post ()
308
+ {
309
+ return $ this ->belongsTo (Post::class);
310
+ }
311
+
312
+ public function video ()
313
+ {
314
+ return $ this ->belongsTo (Video::class);
315
+ }
217
316
}
218
317
219
318
class Comment extends Model
@@ -238,10 +337,26 @@ public function commentable()
238
337
}
239
338
}
240
339
340
+ class PostFactory extends Factory
341
+ {
342
+ protected $ model = Post::class;
343
+
344
+ public function definition ()
345
+ {
346
+ return [];
347
+ }
348
+ }
241
349
class Post extends Model
242
350
{
351
+ use HasFactory;
352
+
243
353
public $ timestamps = false ;
244
354
355
+ public function shouldApplyStatus ()
356
+ {
357
+ return false ;
358
+ }
359
+
245
360
public function comments ()
246
361
{
247
362
return $ this ->morphMany (Comment::class, 'commentable ' );
@@ -256,6 +371,16 @@ public function likes()
256
371
{
257
372
return $ this ->morphMany (Like::class, 'likeable ' );
258
373
}
374
+
375
+ protected static function newFactory ()
376
+ {
377
+ return PostFactory::new ();
378
+ }
379
+
380
+ public function tags ()
381
+ {
382
+ return $ this ->hasMany (Tag::class);
383
+ }
259
384
}
260
385
261
386
class Video extends Model
0 commit comments