diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index a019b0ff66e..fc80f3d4b09 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -85,6 +85,13 @@ abstract class Factory */ protected $afterCreating; + /** + * Whether relationships should not be automatically created. + * + * @var bool + */ + protected $expandRelationships = true; + /** * The name of the database connection that will be used to create the models. * @@ -131,6 +138,7 @@ abstract class Factory * @param \Illuminate\Support\Collection|null $afterCreating * @param string|null $connection * @param \Illuminate\Support\Collection|null $recycle + * @param bool $expandRelationships * @return void */ public function __construct( @@ -142,6 +150,7 @@ public function __construct( ?Collection $afterCreating = null, $connection = null, ?Collection $recycle = null, + bool $expandRelationships = true ) { $this->count = $count; $this->states = $states ?? new Collection; @@ -152,6 +161,7 @@ public function __construct( $this->connection = $connection; $this->recycle = $recycle ?? new Collection; $this->faker = $this->withFaker(); + $this->expandRelationships = $expandRelationships; } /** @@ -479,7 +489,9 @@ protected function expandAttributes(array $definition) { return collect($definition) ->map($evaluateRelations = function ($attribute) { - if ($attribute instanceof self) { + if (! $this->expandRelationships && $attribute instanceof self) { + $attribute = null; + } elseif ($attribute instanceof self) { $attribute = $this->getRandomRecycledModel($attribute->modelName())?->getKey() ?? $attribute->recycle($this->recycle)->create()->getKey(); } elseif ($attribute instanceof Model) { @@ -729,6 +741,16 @@ public function count(?int $count) return $this->newInstance(['count' => $count]); } + /** + * Disable the creation of relationship factories. + * + * @return static + */ + public function withoutRelationships() + { + return $this->newInstance(['expandRelationships' => false]); + } + /** * Get the name of the database connection that is used to generate models. * @@ -767,6 +789,7 @@ protected function newInstance(array $arguments = []) 'afterCreating' => $this->afterCreating, 'connection' => $this->connection, 'recycle' => $this->recycle, + 'expandRelationships' => $this->expandRelationships, ], $arguments))); } diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php index cc3c7850455..2c247a20ae1 100644 --- a/tests/Database/DatabaseEloquentFactoryTest.php +++ b/tests/Database/DatabaseEloquentFactoryTest.php @@ -820,6 +820,15 @@ public function test_no_models_can_be_provided_to_recycle() $this->assertSame(2, FactoryTestUser::count()); } + public function test_can_disable_relationships() + { + $post = FactoryTestPostFactory::new() + ->withoutRelationships() + ->make(); + + $this->assertNull($post->user_id); + } + /** * Get a database connection instance. *