diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 30607e7c45fc..a41e211764e2 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -133,6 +133,13 @@ abstract class Factory */ protected static $factoryNameResolver; + /** + * Whether to expand relationships by default. + * + * @var bool + */ + protected static $expandRelationshipsByDefault = true; + /** * Create a new factory instance. * @@ -144,7 +151,7 @@ abstract class Factory * @param \Illuminate\Support\Collection|null $afterCreating * @param string|null $connection * @param \Illuminate\Support\Collection|null $recycle - * @param bool $expandRelationships + * @param bool|null $expandRelationships */ public function __construct( $count = null, @@ -155,7 +162,7 @@ public function __construct( ?Collection $afterCreating = null, $connection = null, ?Collection $recycle = null, - bool $expandRelationships = true + ?bool $expandRelationships = null ) { $this->count = $count; $this->states = $states ?? new Collection; @@ -166,7 +173,7 @@ public function __construct( $this->connection = $connection; $this->recycle = $recycle ?? new Collection; $this->faker = $this->withFaker(); - $this->expandRelationships = $expandRelationships; + $this->expandRelationships = $expandRelationships ?? self::$expandRelationshipsByDefault; } /** @@ -895,6 +902,26 @@ public static function guessFactoryNamesUsing(callable $callback) static::$factoryNameResolver = $callback; } + /** + * Specify that relationships should create parent relationships by default. + * + * @return void + */ + public static function expandRelationshipsByDefault() + { + static::$expandRelationshipsByDefault = true; + } + + /** + * Specify that relationships should not create parent relationships by default. + * + * @return void + */ + public static function dontExpandRelationshipsByDefault() + { + static::$expandRelationshipsByDefault = false; + } + /** * Get a new Faker instance. * @@ -955,6 +982,7 @@ public static function flushState() static::$modelNameResolvers = []; static::$factoryNameResolver = null; static::$namespace = 'Database\\Factories\\'; + static::$expandRelationshipsByDefault = true; } /** diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php index da5467794d77..a78c792aa303 100644 --- a/tests/Database/DatabaseEloquentFactoryTest.php +++ b/tests/Database/DatabaseEloquentFactoryTest.php @@ -44,6 +44,7 @@ protected function setUp(): void $db->setAsGlobal(); $this->createSchema(); + Factory::expandRelationshipsByDefault(); } /** @@ -831,6 +832,18 @@ public function test_can_disable_relationships() $this->assertNull($post->user_id); } + public function test_can_default_to_without_parents() + { + FactoryTestPostFactory::dontExpandRelationshipsByDefault(); + + $post = FactoryTestPostFactory::new()->make(); + $this->assertNull($post->user_id); + + FactoryTestPostFactory::expandRelationshipsByDefault(); + $postWithParents = FactoryTestPostFactory::new()->create(); + $this->assertNotNull($postWithParents->user_id); + } + public function test_factory_model_names_correct() { $this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);