Skip to content

[12.x] Allow globally disabling Factory parent relationships via Factory::dontExpandRelationshipsByDefault() #56154

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

Merged
merged 5 commits into from
Jul 8, 2025
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
34 changes: 31 additions & 3 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ abstract class Factory
*/
protected static $factoryNameResolver;

/**
* Whether to expand relationships by default.
*
* @var bool
*/
protected static $expandRelationshipsByDefault = true;

/**
* Create a new factory instance.
*
Expand All @@ -144,7 +151,7 @@ abstract class Factory
* @param \Illuminate\Support\Collection|null $afterCreating
* @param string|null $connection
* @param \Illuminate\Support\Collection|null $recycle
* @param bool $expandRelationships
* @param bool|null $expandRelationships
*/
public function __construct(
$count = null,
Expand All @@ -155,7 +162,7 @@ public function __construct(
?Collection $afterCreating = null,
$connection = null,
?Collection $recycle = null,
bool $expandRelationships = true
?bool $expandRelationships = null
) {
$this->count = $count;
$this->states = $states ?? new Collection;
Expand All @@ -166,7 +173,7 @@ public function __construct(
$this->connection = $connection;
$this->recycle = $recycle ?? new Collection;
$this->faker = $this->withFaker();
$this->expandRelationships = $expandRelationships;
$this->expandRelationships = $expandRelationships ?? self::$expandRelationshipsByDefault;
}

/**
Expand Down Expand Up @@ -895,6 +902,26 @@ public static function guessFactoryNamesUsing(callable $callback)
static::$factoryNameResolver = $callback;
}

/**
* Specify that relationships should create parent relationships by default.
*
* @return void
*/
public static function expandRelationshipsByDefault()
{
static::$expandRelationshipsByDefault = true;
}

/**
* Specify that relationships should not create parent relationships by default.
*
* @return void
*/
public static function dontExpandRelationshipsByDefault()
{
static::$expandRelationshipsByDefault = false;
}

/**
* Get a new Faker instance.
*
Expand Down Expand Up @@ -955,6 +982,7 @@ public static function flushState()
static::$modelNameResolvers = [];
static::$factoryNameResolver = null;
static::$namespace = 'Database\\Factories\\';
static::$expandRelationshipsByDefault = true;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function setUp(): void
$db->setAsGlobal();

$this->createSchema();
Factory::expandRelationshipsByDefault();
}

/**
Expand Down Expand Up @@ -831,6 +832,18 @@ public function test_can_disable_relationships()
$this->assertNull($post->user_id);
}

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

$post = FactoryTestPostFactory::new()->make();
$this->assertNull($post->user_id);

FactoryTestPostFactory::expandRelationshipsByDefault();
$postWithParents = FactoryTestPostFactory::new()->create();
$this->assertNotNull($postWithParents->user_id);
}

public function test_factory_model_names_correct()
{
$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);
Expand Down