Skip to content

[12.x] Feat: ability to explicitly exclude factory relationships #56396

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

Draft
wants to merge 5 commits into
base: 12.x
Choose a base branch
from
Draft
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
19 changes: 16 additions & 3 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ abstract class Factory
*/
protected $expandRelationships = true;

/**
* The relationships that should not be automatically created.
*
* @var array
*/
protected $excludeRelationships = [];

/**
* The name of the database connection that will be used to create the models.
*
Expand Down Expand Up @@ -152,6 +159,7 @@ abstract class Factory
* @param string|null $connection
* @param \Illuminate\Support\Collection|null $recycle
* @param bool|null $expandRelationships
* @param array $excludeRelationships
*/
public function __construct(
$count = null,
Expand All @@ -162,7 +170,8 @@ public function __construct(
?Collection $afterCreating = null,
$connection = null,
?Collection $recycle = null,
?bool $expandRelationships = null
?bool $expandRelationships = null,
array $excludeRelationships = [],
) {
$this->count = $count;
$this->states = $states ?? new Collection;
Expand All @@ -174,6 +183,7 @@ public function __construct(
$this->recycle = $recycle ?? new Collection;
$this->faker = $this->withFaker();
$this->expandRelationships = $expandRelationships ?? self::$expandRelationshipsByDefault;
$this->excludeRelationships = $excludeRelationships;
}

/**
Expand Down Expand Up @@ -508,6 +518,8 @@ protected function expandAttributes(array $definition)
->map($evaluateRelations = function ($attribute) {
if (! $this->expandRelationships && $attribute instanceof self) {
$attribute = null;
} elseif ($attribute instanceof self && in_array($attribute->modelName(), $this->excludeRelationships)) {
$attribute = null;
} elseif ($attribute instanceof self) {
$attribute = $this->getRandomRecycledModel($attribute->modelName())?->getKey()
?? $attribute->recycle($this->recycle)->create()->getKey();
Expand Down Expand Up @@ -776,9 +788,9 @@ public function count(?int $count)
*
* @return static
*/
public function withoutParents()
public function withoutParents($parents = [])
{
return $this->newInstance(['expandRelationships' => false]);
return $this->newInstance(! $parents ? ['expandRelationships' => false] : ['excludeRelationships' => $parents]);
}

/**
Expand Down Expand Up @@ -820,6 +832,7 @@ protected function newInstance(array $arguments = [])
'connection' => $this->connection,
'recycle' => $this->recycle,
'expandRelationships' => $this->expandRelationships,
'excludeRelationships' => $this->excludeRelationships,
], $arguments)));
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,16 @@ public function test_can_disable_relationships()
$this->assertNull($post->user_id);
}

public function test_can_disable_relationships_explicitly()
{
$comment = FactoryTestCommentFactory::new()
->withoutParents([FactoryTestUser::class])
->make();

$this->assertNull($comment->user_id);
$this->assertNotNull($comment->commentable->id);
}

public function test_can_default_to_without_parents()
{
FactoryTestPostFactory::dontExpandRelationshipsByDefault();
Expand Down