Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

michaellatham's avatar

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.

0 likes
8 replies
D. Eikelboom's avatar

@Sergiu17 Thanks! Also works with totally different namespace. I've got a src folder with all files in a domains namespace. Only thing I had to also change is the $model property in the factory. Because it was trying to find the model in a wrong namespace.

1 like
michaellatham's avatar

Thanks, that's the approach I took in the end, matching the structure inside the factories folder.

1 like
eballeste's avatar

Hi! I was upgrading today as well and ran into this problem when using custom namespaces. My initial gut instinct was to do what you guys mentioned and just match the namespace of the factories with that of the models but it's kind of weird and doesn't make much sense to me to have to do so.

I know you guys stated that we don't have to overwrite the factoryNameResolver but I'm looking at this code and it looks like we can? What is static::$factoryNameResolver?

1 like
eballeste's avatar

figured this out with a custom trait and adding a callback to the guessFactoryNamesUsing static method of the Factory before calling the factoryForModel static method:

     /**
     * Get a new factory instance for the model.
     *
     * @param  mixed  $parameters
     * @return \Illuminate\Database\Eloquent\Factories\Factory
     */
    public static function factory(...$parameters)
    {
        Factory::guessFactoryNamesUsing(function (string $modelName) {
            // your custom logic to extract the classname from the fully resolved class name
           $modelName = ... ;
 
	   return 'Database\\Factories\\'.$modelName.'Factory';
        });

        return Factory::factoryForModel(get_called_class())
            ->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : null)
            ->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? []));
    }
1 like
wsamoht's avatar

@eballeste I like the idea of the custom trait as you can just add the trait without any extra code.

Another option is to use the existing HasFactory trait and override the newFactory method in your model.

// User model
protected static function newFactory()
{
    return UserFactory::new();
}
ejunker's avatar
ejunker
Best Answer
Level 2

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';
        });
6 likes

Please or to participate in this conversation.