@@ -133,6 +133,13 @@ abstract class Factory
133
133
*/
134
134
protected static $ factoryNameResolver ;
135
135
136
+ /**
137
+ * Whether to expand relationships by default.
138
+ *
139
+ * @var bool
140
+ */
141
+ protected static $ expandRelationshipsByDefault = true ;
142
+
136
143
/**
137
144
* Create a new factory instance.
138
145
*
@@ -144,7 +151,7 @@ abstract class Factory
144
151
* @param \Illuminate\Support\Collection|null $afterCreating
145
152
* @param string|null $connection
146
153
* @param \Illuminate\Support\Collection|null $recycle
147
- * @param bool $expandRelationships
154
+ * @param bool|null $expandRelationships
148
155
*/
149
156
public function __construct (
150
157
$ count = null ,
@@ -155,7 +162,7 @@ public function __construct(
155
162
?Collection $ afterCreating = null ,
156
163
$ connection = null ,
157
164
?Collection $ recycle = null ,
158
- bool $ expandRelationships = true
165
+ ? bool $ expandRelationships = null
159
166
) {
160
167
$ this ->count = $ count ;
161
168
$ this ->states = $ states ?? new Collection ;
@@ -166,7 +173,7 @@ public function __construct(
166
173
$ this ->connection = $ connection ;
167
174
$ this ->recycle = $ recycle ?? new Collection ;
168
175
$ this ->faker = $ this ->withFaker ();
169
- $ this ->expandRelationships = $ expandRelationships ;
176
+ $ this ->expandRelationships = $ expandRelationships ?? self :: $ expandRelationshipsByDefault ;
170
177
}
171
178
172
179
/**
@@ -905,6 +912,26 @@ public static function guessFactoryNamesUsing(callable $callback)
905
912
static ::$ factoryNameResolver = $ callback ;
906
913
}
907
914
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
+
908
935
/**
909
936
* Get a new Faker instance.
910
937
*
@@ -965,6 +992,7 @@ public static function flushState()
965
992
static ::$ modelNameResolvers = [];
966
993
static ::$ factoryNameResolver = null ;
967
994
static ::$ namespace = 'Database \\Factories \\' ;
995
+ static ::$ expandRelationshipsByDefault = true ;
968
996
}
969
997
970
998
/**
0 commit comments