You don't have to overwrite anything, you are still in App namespace, https://i.imgur.com/x1Ev45C.png
Laravel 8 model factory namespaces
I have my models in an unorthodox namespace, similar to Spatie's beyond-crud course.
eg.
App\Domain\Shared\Models\User
The issue i now face in Laravel 8 is that there seems to be a function in Illuminate\Database\Eloquent\Factories\Factory which is the base class factories extend, that resolves the factory class name:
public static function resolveFactoryName(string $modelName)
{
$resolver = static::$factoryNameResolver ?: function (string $modelName) {
$modelName = Str::startsWith($modelName, 'App\Models\')
? Str::after($modelName, 'App\Models\')
: Str::after($modelName, 'App\');
return static::$namespace . $modelName . 'Factory';
};
return $resolver($modelName);
}
You can see that by default this assumes the models are located in either App or App\Models
I think in order to override this I will need to use a custom $factoryNameResolver, but i'm not certain how I might do that. Any assistance is appreciated.
If you are not using the App namespace but instead are using a custom namespace then you can put something like this in your AppServiceProvider
Factory::guessFactoryNamesUsing(function (string $modelName) {
return 'Database\Factories\'.class_basename($modelName).'Factory';
});
Please or to participate in this conversation.