Laravel 8,找不到模型工厂类 [英] Laravel 8, Model factory class not found

查看:72
本文介绍了Laravel 8,找不到模型工厂类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当使用laravel 8.x中引入的新模型工厂类时,这个奇怪的问题说laravel无法找到与该模型相对应的工厂.我收到此错误

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 .....

我听过laravel文档,我不知道发生了什么事

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

这是 BusinessUser

<?php

namespace App;

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

class BusinessUser extends Model
{
    use HasFactory;
}

和工厂

<?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"
        ];
    }
}


任何想法或潜在客户都会受到赞赏.

any ideas or leads is greatly appreciated.

推荐答案

如果从以前的版本升级到8,则可能缺少中的 Database \ Factories 命名空间的自动加载指令.composer.json :

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 部分,因此您也可以删除它.

You can also remove the classmap part, since it is no longer needed.

运行 composer dump .

Laravel 8.x文档-升级指南-数据库-播种机和工厂命名空间

这篇关于Laravel 8,找不到模型工厂类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆