Skip to content

Commit 038cde3

Browse files
[11.x] add ability to disable relationships in factories (#53450)
* add ability to disable relationships sometimes when `make`ing or `create`ing a model, we don't actually care about the relationship. this new property and method allow us to turn them all off at once. when disabled, any attribute that is assigned a factory will instead return `null`. * change method name per @taylorotwell * rename property and invert logic * persist `withoutRelationships` across new statics * refactor * Update Factory.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 6a491c2 commit 038cde3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ abstract class Factory
8585
*/
8686
protected $afterCreating;
8787

88+
/**
89+
* Whether relationships should not be automatically created.
90+
*
91+
* @var bool
92+
*/
93+
protected $expandRelationships = true;
94+
8895
/**
8996
* The name of the database connection that will be used to create the models.
9097
*
@@ -131,6 +138,7 @@ abstract class Factory
131138
* @param \Illuminate\Support\Collection|null $afterCreating
132139
* @param string|null $connection
133140
* @param \Illuminate\Support\Collection|null $recycle
141+
* @param bool $expandRelationships
134142
* @return void
135143
*/
136144
public function __construct(
@@ -142,6 +150,7 @@ public function __construct(
142150
?Collection $afterCreating = null,
143151
$connection = null,
144152
?Collection $recycle = null,
153+
bool $expandRelationships = true
145154
) {
146155
$this->count = $count;
147156
$this->states = $states ?? new Collection;
@@ -152,6 +161,7 @@ public function __construct(
152161
$this->connection = $connection;
153162
$this->recycle = $recycle ?? new Collection;
154163
$this->faker = $this->withFaker();
164+
$this->expandRelationships = $expandRelationships;
155165
}
156166

157167
/**
@@ -479,7 +489,9 @@ protected function expandAttributes(array $definition)
479489
{
480490
return collect($definition)
481491
->map($evaluateRelations = function ($attribute) {
482-
if ($attribute instanceof self) {
492+
if (! $this->expandRelationships && $attribute instanceof self) {
493+
$attribute = null;
494+
} elseif ($attribute instanceof self) {
483495
$attribute = $this->getRandomRecycledModel($attribute->modelName())?->getKey()
484496
?? $attribute->recycle($this->recycle)->create()->getKey();
485497
} elseif ($attribute instanceof Model) {
@@ -729,6 +741,16 @@ public function count(?int $count)
729741
return $this->newInstance(['count' => $count]);
730742
}
731743

744+
/**
745+
* Disable the creation of relationship factories.
746+
*
747+
* @return static
748+
*/
749+
public function withoutRelationships()
750+
{
751+
return $this->newInstance(['expandRelationships' => false]);
752+
}
753+
732754
/**
733755
* Get the name of the database connection that is used to generate models.
734756
*
@@ -767,6 +789,7 @@ protected function newInstance(array $arguments = [])
767789
'afterCreating' => $this->afterCreating,
768790
'connection' => $this->connection,
769791
'recycle' => $this->recycle,
792+
'expandRelationships' => $this->expandRelationships,
770793
], $arguments)));
771794
}
772795

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,15 @@ public function test_no_models_can_be_provided_to_recycle()
820820
$this->assertSame(2, FactoryTestUser::count());
821821
}
822822

823+
public function test_can_disable_relationships()
824+
{
825+
$post = FactoryTestPostFactory::new()
826+
->withoutRelationships()
827+
->make();
828+
829+
$this->assertNull($post->user_id);
830+
}
831+
823832
/**
824833
* Get a database connection instance.
825834
*

0 commit comments

Comments
 (0)