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

cooperino's avatar

PHP Error: Class 'Database\Factories\MyFactory' not found

I am using Laravel 8 and not upgraded from Laravel 7. I have a model in Models/MyModel and I created a factory for it

when I run App\Models\MyModel::factory()->create(), I get:

PHP Error:  Class 'Database\Factories\MyFactory' not found

The files exist

0 likes
4 replies
knubbe's avatar

In factory class you must define $model property with corresponding model. In your case:

class MyFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = MyModel::class;
}

1 like
cooperino's avatar

@knubbe Yes! I came here to say that I ran composer dump-autoload and it worked but you just said it as well, thank you!

Please or to participate in this conversation.