I want to seed database when I use this

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

Add three user in database but when I use this

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

显示错误

[InvalidArgumentException]
Unable to locate factory with name [default] [app\Comment].

推荐答案

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

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

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.

因此,您的注释Model Factory将如下所示:

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

Laravel相关问答推荐

灯丝:设定模式标题

如何在Vite和Rollup中完全禁用分块?

如何防止 Laravel 路由被直接访问(即非 ajax 请求)

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

Laravel/Eloquent:致命错误:在非对象上调用成员函数 connection()

Laravel:Blade foreach 循环 bootstrap 列

Laravel 5 make:控制器在应用程序文件夹而不是控制器文件夹中创建控制器

在 laravel 如何将额外的数据传递给 mutators 和 accessors

如何更改 Laravel5 中的视图文件夹?

查询构建器中的 Laravel 5 多重 Select ()

Laravel Artisan Scheduler 中的链式命令?

使用模型工厂、一对一和一对多关系定义 Laravel 外键,而不创建不必要的模型

Http请求多浏览器的烦恼

Laravel 5 环境配置数组?

Laravel Passport 通过访问令牌获取客户端 ID

如何在 Laravel 视图中找到当前语言?

Laravel 事件、监听器、作业(job)、队列之间的区别

laravel 如何读取 app/config/app.php 调试变量

在 vue.js 中使用 Laravel 的Gate/Authorization

如何在 Laravel 中显示动态复选框的旧数据?