Skip to content

Commit

Permalink
[11.x] add ability to disable relationships in factories (#53450)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>
  • Loading branch information
browner12 and taylorotwell authored Nov 18, 2024
1 parent 6a491c2 commit 038cde3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -152,6 +161,7 @@ public function __construct(
$this->connection = $connection;
$this->recycle = $recycle ?? new Collection;
$this->faker = $this->withFaker();
$this->expandRelationships = $expandRelationships;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -767,6 +789,7 @@ protected function newInstance(array $arguments = [])
'afterCreating' => $this->afterCreating,
'connection' => $this->connection,
'recycle' => $this->recycle,
'expandRelationships' => $this->expandRelationships,
], $arguments)));
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 038cde3

Please sign in to comment.