Skip to content

Commit 9f71c7b

Browse files
[12.x] Allow globally disabling Factory parent relationships via Factory::dontExpandRelationshipsByDefault() (#56154)
* default expanding relationships static property * flush * flush in test * static method * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 474bf78 commit 9f71c7b

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ abstract class Factory
133133
*/
134134
protected static $factoryNameResolver;
135135

136+
/**
137+
* Whether to expand relationships by default.
138+
*
139+
* @var bool
140+
*/
141+
protected static $expandRelationshipsByDefault = true;
142+
136143
/**
137144
* Create a new factory instance.
138145
*
@@ -144,7 +151,7 @@ abstract class Factory
144151
* @param \Illuminate\Support\Collection|null $afterCreating
145152
* @param string|null $connection
146153
* @param \Illuminate\Support\Collection|null $recycle
147-
* @param bool $expandRelationships
154+
* @param bool|null $expandRelationships
148155
*/
149156
public function __construct(
150157
$count = null,
@@ -155,7 +162,7 @@ public function __construct(
155162
?Collection $afterCreating = null,
156163
$connection = null,
157164
?Collection $recycle = null,
158-
bool $expandRelationships = true
165+
?bool $expandRelationships = null
159166
) {
160167
$this->count = $count;
161168
$this->states = $states ?? new Collection;
@@ -166,7 +173,7 @@ public function __construct(
166173
$this->connection = $connection;
167174
$this->recycle = $recycle ?? new Collection;
168175
$this->faker = $this->withFaker();
169-
$this->expandRelationships = $expandRelationships;
176+
$this->expandRelationships = $expandRelationships ?? self::$expandRelationshipsByDefault;
170177
}
171178

172179
/**
@@ -905,6 +912,26 @@ public static function guessFactoryNamesUsing(callable $callback)
905912
static::$factoryNameResolver = $callback;
906913
}
907914

915+
/**
916+
* Specify that relationships should create parent relationships by default.
917+
*
918+
* @return void
919+
*/
920+
public static function expandRelationshipsByDefault()
921+
{
922+
static::$expandRelationshipsByDefault = true;
923+
}
924+
925+
/**
926+
* Specify that relationships should not create parent relationships by default.
927+
*
928+
* @return void
929+
*/
930+
public static function dontExpandRelationshipsByDefault()
931+
{
932+
static::$expandRelationshipsByDefault = false;
933+
}
934+
908935
/**
909936
* Get a new Faker instance.
910937
*
@@ -965,6 +992,7 @@ public static function flushState()
965992
static::$modelNameResolvers = [];
966993
static::$factoryNameResolver = null;
967994
static::$namespace = 'Database\\Factories\\';
995+
static::$expandRelationshipsByDefault = true;
968996
}
969997

970998
/**

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected function setUp(): void
4444
$db->setAsGlobal();
4545

4646
$this->createSchema();
47+
Factory::expandRelationshipsByDefault();
4748
}
4849

4950
/**
@@ -831,6 +832,18 @@ public function test_can_disable_relationships()
831832
$this->assertNull($post->user_id);
832833
}
833834

835+
public function test_can_default_to_without_parents()
836+
{
837+
FactoryTestPostFactory::dontExpandRelationshipsByDefault();
838+
839+
$post = FactoryTestPostFactory::new()->make();
840+
$this->assertNull($post->user_id);
841+
842+
FactoryTestPostFactory::expandRelationshipsByDefault();
843+
$postWithParents = FactoryTestPostFactory::new()->create();
844+
$this->assertNotNull($postWithParents->user_id);
845+
}
846+
834847
public function test_factory_model_names_correct()
835848
{
836849
$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);

0 commit comments

Comments
 (0)