問(wèn)題描述
我想播種數(shù)據(jù)庫(kù)當(dāng)我使用這個(gè)
I want to seed database when I use this
public function run()
{
$users = factory(appUser::class, 3)->create();
}
在數(shù)據(jù)庫(kù)中添加三個(gè)用戶(hù)但是當(dāng)我使用這個(gè)
Add three user in database but when I use this
public function run()
{
$Comment= factory(appComment::class, 3)->create();
}
顯示錯(cuò)誤
[無(wú)效參數(shù)異常]
無(wú)法找到名稱(chēng)為 [default] [appComment] 的工廠(chǎng).
[InvalidArgumentException]
Unable to locate factory with name [default] [appComment].
推薦答案
默認(rèn)情況下,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),
];
});
因此您需要先定義一個(gè)工廠(chǎng)模型,然后再使用它來(lái)為數(shù)據(jù)庫(kù)做種.這只是使用 Faker Library 的一個(gè)實(shí)例,它用于生成假數(shù)據(jù)以播種數(shù)據(jù)庫(kù)以執(zhí)行測(cè)試.
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.
確保您已為評(píng)論模型添加了類(lèi)似的模態(tài)工廠(chǎng).
Make sure You have added a similar Modal Factory for the Comments Model.
所以你的評(píng)論模型工廠(chǎng)將是這樣的:
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
];
});
這篇關(guān)于Laravel 5.2:無(wú)法找到名稱(chēng)為 [默認(rèn)] 的工廠(chǎng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!