Laravel 8, Model factory class not found

If you upgraded to 8 from a previous version you are probably missing the autoload directive for the Database\Factories namespace in composer.json:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},

You can also remove the classmap part, since it is no longer needed.

Run composer dump after making these changes.

Laravel 8.x Docs - Upgrade Guide - Database - Seeder and Factory Namespace


I'm in the process of migrating from laravel 7 to 8.

After banging my head against the wall for a while and looking at the source code, I saw that you can optionally override what factory class gets called for a model using the newFactory method on the model.

I also then noticed that it IS in the documentation (https://laravel.com/docs/8.x/database-testing#creating-models) - I just didn't understand what it meant the first time I read it. Now I do.

I solved this by the following:

<?php

namespace My\Fancy\Models;

use Database\Factories\SomeFancyFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SomeClass extends Model
{
    use HasFactory;

    /** @return SomeFancyFactory */
    protected static function newFactory()
    {
        return SomeFancyFactory::new();
    }
}

After this change, my tests passed as expected.


You need to ensure that the namespace is the same: as shown below: otherwise this will screw you up big time. The name of the factory is the name of the model + Factory

e.g. app\models\User- will match to database/factories/UserFactory

finally ensure you run: composer dumpautoload


Apparently you have to respect the folder structure as well. For example, if you have the User Model in the following path: app\Models\Users\User, then the respective factory should be located in database\factories\Users\UserFactory.

Tags:

Php

Laravel