沃梦达 / 编程问答 / php问题 / 正文

Laravel 5.2:无法找到名称为 [默认] 的工厂

Laravel 5.2: Unable to locate factory with name [default](Laravel 5.2:无法找到名称为 [默认] 的工厂)

本文介绍了Laravel 5.2:无法找到名称为 [默认] 的工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想播种数据库当我使用这个

I want to seed database when I use this

 public function run()
{
    $users = factory(appUser::class, 3)->create();
}

在数据库中添加三个用户但是当我使用这个

Add three user in database but when I use this

 public function run()
{
    $Comment= factory(appComment::class, 3)->create();
}

显示错误

[无效参数异常]
无法找到名称为 [default] [appComment] 的工厂.

[InvalidArgumentException]
Unable to locate factory with name [default] [appComment].

推荐答案

默认情况下,laravel 安装在 database/factories/ModelFactory.php 文件中带有此代码.

By default the laravel installation comes with this code in the database/factories/ModelFactory.php File.

$factory->define(AppUser::class, function (FakerGenerator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

因此您需要先定义一个工厂模型,然后再使用它来为数据库做种.这只是使用 Faker Library 的一个实例,它用于生成假数据以播种数据库以执行测试.

So you need to define a factory Model before you use it to seed database. This just uses an instance of Faker Library which is used to generate fake Data for seeding the database to perform testing.

确保您已为评论模型添加了类似的模态工厂.

Make sure You have added a similar Modal Factory for the Comments Model.

所以你的评论模型工厂将是这样的:

So your Comments Model Factory will be something like this :

$factory->define(AppComment::class, function (FakerGenerator $faker) {
    return [
        'comment' => $faker->sentence,
         // Any other Fields in your Comments Model 
    ];
});

这篇关于Laravel 5.2:无法找到名称为 [默认] 的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5.2:无法找到名称为 [默认] 的工厂

基础教程推荐