php – Laravel 5.2:无法找到名称为[factory]的工厂
作者:互联网
我想种子数据库
当我用这个
public function run()
{
$users = factory(app\User::class, 3)->create();
}
在数据库中添加三个用户,但是当我使用它时
public function run()
{
$Comment= factory(app\Comment::class, 3)->create();
}
告诉我错误
[InvalidArgumentException]
Unable to locate factory with name [default] [app\Comment].
解决方法:
默认情况下,laravel安装在数据库/ factories / ModelFactory.php文件中附带此代码.
$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),
];
});
因此,在使用工厂模型对种子数据库进行种子设定之前,需要先定义它.
这仅使用Faker Library的实例,该实例用于生成伪数据以播种数据库以执行测试.
确保您为评论模型添加了类似的模态工厂.
所以你的评论模型工厂将是这样的:
$factory->define(App\Comment::class, function (Faker\Generator $faker) {
return [
'comment' => $faker->sentence,
// Any other Fields in your Comments Model
];
});
标签:laravel-seeding,php,laravel,laravel-5,laravel-5-2 来源: https://codeday.me/bug/20191005/1856319.html