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

wordxpression's avatar

Factories can't be found...

When I create a factory for a model, when I try to seed the records, it does not work with the error message

Class "Database\Factories<modelname>" not found.

I have found a lot of potential causes, but none of them was a solution for my problem.

All the factories I created -and they fail all- follow the <Modelname>Factory form and they're all stored in \Database\Factories.

Since it didn't work out of the box, I tried the following:

  1. I tried to create the factory in a 'newFactory' method of the model,, and assign the model to the factory in a $model property.
  2. I looked at the composer.json file to the autoload property. All was as the documentation expected it to be.
  3. I'm running a NEW project on Laravel 10, so the possible 'problems of older versions' have nothing to do with it.

Does anyone have any other suggestion what might be the cause?

Thanks in advance. Wilko

0 likes
2 replies
LaryAI's avatar
Level 58

Hello Wilko,

It seems like you've already checked some of the common issues that could cause the "Class not found" error for your factories. Here are a few more steps you can take to troubleshoot and resolve the issue:

  1. Namespace Verification: Ensure that the namespace for your factory is correct. It should be Database\Factories. If your model is in a different namespace, you need to import it properly in your factory.

  2. Factory Definition: Make sure that your factory class name matches the convention <ModelName>Factory and extends Illuminate\Database\Eloquent\Factories\Factory. For example, if your model is App\Models\Post, your factory should be:

namespace Database\Factories;

use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
    protected $model = Post::class;

    public function definition()
    {
        return [
            // Your factory definition
        ];
    }
}
  1. Composer Autoload: Run composer dump-autoload to regenerate the autoload files. This ensures that any new classes are properly recognized by the autoloader.

  2. Artisan Commands: Use the artisan command to create the factory if you haven't already. This will ensure that the factory is created with the correct namespace and path.

php artisan make:factory PostFactory --model=Post
  1. Database Seeder: In your database seeder, make sure you are using the correct namespace when referencing the factory. For example:
use Database\Factories\PostFactory;

PostFactory::new()->count(50)->create();

Or if you're using the factory() helper function, ensure you're passing the fully qualified model class name:

\App\Models\Post::factory()->count(50)->create();
  1. Clear Caches: Sometimes, Laravel caches can cause issues. Try clearing the cache using the following commands:
php artisan cache:clear
php artisan config:clear
  1. File Permissions: Ensure that the file permissions are correct and that the Laravel application has the necessary permissions to read the factory files.

  2. File Naming: Check that the file name of your factory matches the class name exactly, including case sensitivity, as Linux systems are case-sensitive.

If after trying all these steps the problem persists, it might be helpful to post the exact factory code and the seeder code so that others can help you debug the issue more effectively.

wordxpression's avatar

Most of the things mentioned I already tried, only not the config:clear. That didn't help either however.

Please or to participate in this conversation.