File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
src/Illuminate/Database/Eloquent/Factories Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 11
11
use Illuminate \Database \Eloquent \SoftDeletes ;
12
12
use Illuminate \Support \Carbon ;
13
13
use Illuminate \Support \Collection ;
14
+ use Illuminate \Support \Enumerable ;
14
15
use Illuminate \Support \Str ;
15
16
use Illuminate \Support \Traits \Conditionable ;
16
17
use Illuminate \Support \Traits \ForwardsCalls ;
@@ -329,7 +330,7 @@ protected function store(Collection $results)
329
330
$ model ->save ();
330
331
331
332
foreach ($ model ->getRelations () as $ name => $ items ) {
332
- if ($ items ->isEmpty ()) {
333
+ if ($ items instanceof Enumerable && $ items ->isEmpty ()) {
333
334
$ model ->unsetRelation ($ name );
334
335
}
335
336
}
Original file line number Diff line number Diff line change @@ -364,6 +364,24 @@ public function test_belongs_to_many_relationship_related_models_set_on_instance
364
364
$ this ->assertCount (1 , $ role ->users );
365
365
}
366
366
367
+ public function test_relation_can_be_loaded_before_model_is_created ()
368
+ {
369
+ $ user = FactoryTestUserFactory::new (['name ' => 'Taylor Otwell ' ])->createOne ();
370
+
371
+ $ post = FactoryTestPostFactory::new ()
372
+ ->for ($ user , 'user ' )
373
+ ->afterMaking (function (FactoryTestPost $ post ) {
374
+ $ post ->load ('user ' );
375
+ })
376
+ ->createOne ();
377
+
378
+ $ this ->assertTrue ($ post ->relationLoaded ('user ' ));
379
+ $ this ->assertTrue ($ post ->user ->is ($ user ));
380
+
381
+ $ this ->assertCount (1 , FactoryTestUser::all ());
382
+ $ this ->assertCount (1 , FactoryTestPost::all ());
383
+ }
384
+
367
385
public function test_belongs_to_many_relationship_with_existing_model_instances ()
368
386
{
369
387
$ roles = FactoryTestRoleFactory::times (3 )
You can’t perform that action at this time.
0 commit comments