Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] add ability to disable relationships in factories #53450

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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