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:无法找到名称为 [默认] 的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)