'Laravel 8 Error No Found : Class "Database\Factories\Backend\AdminFactory"

I have this AdminFactory.php file in database->factories directory:

<?php

namespace Database\Factories;

use App\Models\Backend\Admin;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => 'Admin',
            'email' => '[email protected]',
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

When I try to run this command : php artisan migrate --seed

it display this error message in terminal :

enter image description here



Solution 1:[1]

Ran into this issue when trying to use a factory in a package. I solved it by overriding the HasFactory::newFactory() method.

class LegacyCategory extends Model
{
    use HasFactory;

    ...    

    /**
     * Create a new factory instance for the model.
     *
     * @return \Illuminate\Database\Eloquent\Factories\Factory
     */
    protected static function newFactory()
    {
        return new LegacyCategoryFactory();
    }           
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joanis