File tree Expand file tree Collapse file tree 2 files changed +15
-4
lines changed
src/Illuminate/Database/Eloquent/Factories Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -138,7 +138,7 @@ abstract class Factory
138138 *
139139 * @var bool
140140 */
141- public static $ defaultExpandRelationships = true ;
141+ protected static $ defaultExpandRelationships = true ;
142142
143143 /**
144144 * Create a new factory instance.
@@ -902,6 +902,17 @@ public static function guessFactoryNamesUsing(callable $callback)
902902 static ::$ factoryNameResolver = $ callback ;
903903 }
904904
905+ /**
906+ * Specify that relationships should not create parent relationships.
907+ *
908+ * @param bool $dontExpandRelationships
909+ * @return void
910+ */
911+ public static function dontExpandRelationshipsByDefault (bool $ dontExpandRelationships = true )
912+ {
913+ static ::$ defaultExpandRelationships = ! $ dontExpandRelationships ;
914+ }
915+
905916 /**
906917 * Get a new Faker instance.
907918 *
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ protected function setUp(): void
4444 $ db ->setAsGlobal ();
4545
4646 $ this ->createSchema ();
47- Factory::$ defaultExpandRelationships = true ;
47+ Factory::dontExpandRelationshipsByDefault () ;
4848 }
4949
5050 /**
@@ -834,12 +834,12 @@ public function test_can_disable_relationships()
834834
835835 public function test_can_default_to_without_parents ()
836836 {
837- FactoryTestPostFactory::$ defaultExpandRelationships = false ;
837+ FactoryTestPostFactory::dontExpandRelationshipsByDefault () ;
838838
839839 $ post = FactoryTestPostFactory::new ()->make ();
840840 $ this ->assertNull ($ post ->user_id );
841841
842- FactoryTestPostFactory::$ defaultExpandRelationships = true ;
842+ FactoryTestPostFactory::dontExpandRelationshipsByDefault ( false ) ;
843843 $ postWithParents = FactoryTestPostFactory::new ()->create ();
844844 $ this ->assertNotNull ($ postWithParents ->user_id );
845845 }
You can’t perform that action at this time.
0 commit comments