Laravel Database Seeder Factory Not Found

Laravel Database Seeder Factory Not Found

If you found a class database factory seeder not found error, while working in laravel apps; So in this tutorial, you will learn how to fix class database factory seeder not found error in laravel.

Class Database Seeder Factory Not Found in Laravel

Here are steps to troubleshoot and resolve this issue:

  • Step 1: Verify Factory Exists
  • Step 2: Check Factory Namespace
  • Step 3: Regenerate Autoload Files
  • Step 4: Run Seeder with Class Name
  • Step 5: Check DatabaseSeeder Class
  • Step 6: Composer Dump-Autoload

Step 1: Verify Factory Exists

First of all, open terminal or cmd and run the following command into it to verify factory directory exists or not:

cd your-project

# Check the existence of the factory file
ls database/factories

Step 2: Check Factory Namespace

Laravel follows PSR-4 autoloading standards, and factories are usually under the Database\Factories namespace. Confirm that your factory file includes the correct namespace.

// database/factories/YourModelFactory.php

namespace Database\Factories;

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

class YourModelFactory extends Factory
{
    // Factory definition
}

Step 3: Regenerate Autoload Files

In some cases, the autoload files may not be updated, causing Laravel to be unaware of the new or modified factory. To regenerate the autoload files, run the following commands:

composer dump-autoload

Step 4: Run Seeder with Class Name

While running the seeder, you need to full class name of the seeder, including the namespace. i.e.:

php artisan db:seed --class=YourSeederClassName

Step 5: Check DatabaseSeeder Class

Verify that your DatabaseSeeder class in database/seeders/DatabaseSeeder.php file is correctly using the factories. It should look something like this:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        \App\Models\YourModel::factory(10)->create();
        // Additional factories or seeders
    }
}

Step 6: Composer Dump-Autoload

If you’ve recently created a new factory or seeder, or if you’ve moved files around, Then you need to run the composer dump-autoload command to autoload files:

composer dump-autoload

Conclusion

That’s it; you have learned how to troubleshoot and resolve the ” Database Seeder Factory not found” issue in Laravel.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *