Skip to content

Commit 43bea00

Browse files
committed
Merge remote-tracking branch 'origin/8.x' into 8.x
2 parents bc1cb87 + dd233cf commit 43bea00

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,7 @@ public static function useNamespace(string $namespace)
618618
*/
619619
public static function factoryForModel(string $modelName)
620620
{
621-
$resolver = static::$factoryNameResolver ?: function ($modelName) {
622-
return static::$namespace.Str::singular(class_basename($modelName)).'Factory';
623-
};
624-
625-
$factory = $resolver($modelName);
621+
$factory = static::resolveFactoryName($modelName);
626622

627623
return $factory::new();
628624
}
@@ -648,6 +644,25 @@ protected function withFaker()
648644
return Container::getInstance()->make(Generator::class);
649645
}
650646

647+
/**
648+
* Get the factory name for the given model name.
649+
*
650+
* @param string $modelName
651+
* @return string
652+
*/
653+
public static function resolveFactoryName(string $modelName)
654+
{
655+
$resolver = static::$factoryNameResolver ?: function (string $modelName) {
656+
$modelName = Str::startsWith($modelName, 'App\\Models\\')
657+
? Str::after($modelName, 'App\\Models\\')
658+
: Str::after($modelName, 'App\\');
659+
660+
return static::$namespace.$modelName.'Factory';
661+
};
662+
663+
return $resolver($modelName);
664+
}
665+
651666
/**
652667
* Proxy dynamic factory methods onto their proper methods.
653668
*

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ public function test_sequences()
259259
}));
260260
}
261261

262+
public function test_resolve_nested_model_factories()
263+
{
264+
Factory::useNamespace('Factories\\');
265+
266+
$resolves = [
267+
'App\\Foo' => 'Factories\\FooFactory',
268+
'App\\Models\\Foo' => 'Factories\\FooFactory',
269+
'App\\Models\\Nested\\Foo' => 'Factories\\Nested\\FooFactory',
270+
'App\\Models\\Really\\Nested\\Foo' => 'Factories\\Really\\Nested\\FooFactory',
271+
];
272+
273+
foreach ($resolves as $model => $factory) {
274+
$this->assertEquals($factory, Factory::resolveFactoryName($model));
275+
}
276+
}
277+
262278
public function test_model_has_factory()
263279
{
264280
Factory::guessFactoryNamesUsing(function ($model) {

0 commit comments

Comments
 (0)