close

[Solved] Laravel 8, Model factory class not found

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Laravel 8, Model factory class not found in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Laravel 8, Model factory class not found Error Occurs?

Today I get the following error Laravel 8, Model factory class not found in php.

How To Solve Laravel 8, Model factory class not found Error ?

  1. How To Solve Laravel 8, Model factory class not found Error ?

    To Solve Laravel 8, Model factory class not found Error In my own case, it happened in a Laravel 8 project ie it wasn't a project I upgraded from Laravel 7. And I noticed this after doing composer update recently.

  2. Laravel 8, Model factory class not found

    To Solve Laravel 8, Model factory class not found Error In my own case, it happened in a Laravel 8 project ie it wasn't a project I upgraded from Laravel 7. And I noticed this after doing composer update recently.

Solution 1

Add this to AppServiceProvider::boot() to prevent namespace of model guessing.

Factory::guessFactoryNamesUsing(function (string $modelName) {
        return 'Database\\Factories\\' . Arr::last(explode('\\', $modelName)) . 'Factory';
    });

Solution 2

In my own case, it happened in a Laravel 8 project ie it wasn’t a project I upgraded from Laravel 7. And I noticed this after doing composer update recently.

1: When creating the model, create the factory alongside

php artisan make:model BusinessUser -f // -f creates the factory

2: For your older models comment out use HasFactory; or just create the factory

php artisan make:factory BusinessUserFactory -m

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment