so when using the new model factories class introduced in laravel 8.x, ive this weird issue saying that laravel cannot find the factory that corresponds to the model. i get this error

PHP Error:  Class 'Database/Factories/BusinessUserFactory' not found in .....

tho ive followed the laravel docs, ive no idea whats going on

Here is the BusinessUser class

<?php

namespace App;

use Database\Factories\BusinessUserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BusinessUser extends Model
{
    use HasFactory;
}

and the factory

<?php

namespace Database\Factories;

use App\BusinessUser;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => "dsfsf"
        ];
    }
}


任何 idea 或线索都将不胜感激.

推荐答案

If you upgraded to 8 from a previous version you are probably missing the autoload directive for the Database\Factories namespace in composer.json:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},

您也可以移除classmap零件,因为它不再需要.

Run composer dump after making these changes.

Laravel 8.x Docs - Upgrade Guide - Database - Seeder and Factory Namespace

Laravel相关问答推荐

从8.0更新到10.0后,图像不再上传到存储

如何在控制器中获取所需参数?

Laravel 数据未传递到下拉框

vagrant会损坏你的电脑. | macOS v12(蒙特雷)#13132

使用两个日期之间的范围获取两列之间的记录,同时搜索过滤条件

Laravel 验证 - 不同的属性规范

如何获得每种类型的总和

使用 Laravel 计算页面浏览量

Laravel - 如果值包含某个字符串(取自搜索输入),则查询模型

Laravel 保存一对多关系

查询 Laravel Select WhereIn 数组

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

Laravel - 排序的集合输出不是数组

Laravel Eloquent Pluck 不丢失密钥

Twilio 查找 API 不起作用?

Laravel 应用程序中应用程序键的意义是什么?

如何从不是控制器方法的方法发送响应?

如何在laravel 5.1中使用url(路由)传递多个参数

Laravel 5.4 - Mix - 如何运行浏览器实时重新加载

如何在 Laravel 中使用 SQL Server 连接?