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

zot24's avatar

[L5.1] How to change factories path when using Model Factories

I saw that there is a static constructor on the Illuminate\Database\Eloquent\Factory class

...
public static function construct($pathToFactories = null)
{
    $pathToFactories = $pathToFactories ?: database_path('factories');
...

However how can I change where my factories are stored? I have a different path for them than the default one database/factories

I know that there was an option back when using TestDummy: how-do-i-specify-a-different-factories-folder

But don't know how to change that path now on 5.1

0 likes
4 replies
zot24's avatar

Just for the record I've been trying to bind the creation of the Illuminate\Database\Eloquent\Factory in an service provider as below:

use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\ServiceProvider;

class FactoryServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->registerFactory();
    }

    public function registerFactory()
    {
        $this->app->bind(Factory::class, function () {
            return Factory::construct(env('FACTORIES_PATH', '../database/factories'));
        });
    }
}

But that doesn't works

phildawson's avatar
Level 26

@zot24 That's weird I was just about to post this before you updated with your second post.

$this->app->singleton(Factory::class, function () {
    return Factory::construct(env('FACTORIES_PATH', database_path('factories')));
});

Edit:

Yeah I've just tested the below in routes renaming the factories folder to foo as a quick test and works a treat.

ServiceProvider works fine too.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;

class FactoryServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Factory::class, function () {
            return Factory::construct(database_path('foo'));
        });
    }
}

It would work with bind too (even though it should be shared), so I'm not sure why yours isn't working unless its the obvious of not adding the service provider to app/config.php

1 like
zot24's avatar

Not it wasn't that, it was a mistake importing the wrong Factory on the service provider, thanks it works fine for me with the service provider.

However don't you think is a little bit overkilling create a service provider just for this purpose I wish there could be another way as it was when using the TestDummy:

Factory::$factoriesPath = 'app/tests/factories';

I just want the factories for my test don't wanna add code on my app that is just for testing purposes.

Edit:

I just added the following:

 $this->app->singleton(Factory::class, function () {
       return Factory::construct(env('FACTORIES_PATH', '../database/factories'));
    });

To my setUp method instead of creating a service provider specific for this purpose and works I think this is a cleaner way to do it, at least in my case.

Thanks @phildawson

1 like
mahmoudz's avatar

That's what worked for me:

Call this from your service provider Register function


    private function changeTheDefaultDatabaseModelsFactoriesPath()
    {
        $customPath = '/src/Infrastructure/Fakers';

        $this->app->singleton(\Illuminate\Database\Eloquent\Factory::class, function ($app) use ($customPath) {
            $faker = $app->make(\Faker\Generator::class);
            return \Illuminate\Database\Eloquent\Factory::construct($faker, base_path() . $customPath);
        });

    }

Please or to participate in this conversation.