Laravel 5.1 - 模型工厂错误种子
Posted
技术标签:
【中文标题】Laravel 5.1 - 模型工厂错误种子【英文标题】:Laravel 5.1 - Model Factory Error seed 【发布时间】:2016-10-17 23:04:00 【问题描述】:您好,我正在尝试使用一些文章、BlogCategories、用户来填充我的数据库。
当我执行“php artisan db:seed”时出现此错误:
[ErrorException] 参数 2 传递给 Illuminate\Database\Eloquent\Factory::define() 必须是可调用的。 给定的字符串,调用 C:\xampp\htdocs\2016\database\factories\ModelFactory.php 在第 22 行 并定义
我在我的模型中插入了所有关系,并且我的迁移做得很好!
ModelFactory.php
$factory->define(dixard\User::class, 'admin', function (Faker\Generator $faker)
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt('123'),
'user_type' => 3,
'remember_token' => str_random(10),
];
); // line 22
$factory->define(dixard\User::class, 'member', function (Faker\Generator $faker)
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt('123'),
'user_type' => 1,
'remember_token' => str_random(10),
];
);
$factory->define(dixard\BlogCategory::class, function (Faker\Generator $faker)
return [
'name' => $faker->word,
];
);
$factory->define(dixard\Article::class, function (Faker\Generator $faker)
return [
'title' => $faker->sentence,
'content' => $faker->paragraph,
'tags' => $faker->word,
'user_id' => dixard\User::all()->random()->id,
'category_id' => dixard\BlogCategory::all()->random()->id,
];
);
user_type 可以是 0(客户)、1(艺术家)、3(管理员)是一个布尔字段。
databaseSeeder.php
public function run()
Model::unguard();
factory('dixard\User','admin', 3)->create();
factory('dixard\BlogCategory', 5)->create();
factory('dixard\Article', 20)->create();
// $this->call(UserTableSeeder::class);
$this->call(CategoryTableSeeder::class);
$this->call(GenderTableSeeder::class);
$this->call(ProductTableSeeder::class);
$this->call(ColorTableSeeder::class);
$this->call(BalanceTableSeeder::class);
$this->call(ShippingsTableSeeder::class);
$this->call(CouponTableSeeder::class);
Model::reguard();
感谢您的帮助!
【问题讨论】:
define 方法中的第二个参数是什么? 【参考方案1】:我相信您的前两种方法是使用defineAs()
而不是define()
:
https://laravel.com/docs/5.1/testing § "多种工厂类型"
$factory->defineAs(dixard\User::class, 'admin', function (Faker\Generator $faker)
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt('123'),
'user_type' => 3,
'remember_token' => str_random(10),
];
); // line 22
$factory->defineAs(dixard\User::class, 'member', function (Faker\Generator $faker)
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt('123'),
'user_type' => 1,
'remember_token' => str_random(10),
];
);
【讨论】:
【参考方案2】:您需要像这样重新定义您的 ModelFactory。
$factory->define(dixard\User::class, function (Faker\Generator $faker)
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt('123'),
'remember_token' => str_random(10),
'user_type' = 1
];
);
然后在您的 DatabaseSeeder 文件中:
factory('dixard\User', 3)->create(['user_type'=>3]); // Create 3 admin users
factory('dixard\User', 3)->create(); // By default it will create 3 customer users
factory('dixard\BlogCategory', 5)->create();
factory('dixard\Article', 20)->create();
【讨论】:
【参考方案3】:代替
$factory->define(dixard\User::class, 'admin', function (Faker\Generator $faker)
只需删除每个方法的“生成器”,它应该是这样的:
$factory->define(dixard\User::class, 'admin', function (Faker $faker)
【讨论】:
根据已接受答案中链接的文档,我认为这不是正确的解决方案。以上是关于Laravel 5.1 - 模型工厂错误种子的主要内容,如果未能解决你的问题,请参考以下文章
laravel 5.1 Artisan::call() 参数--供应商标签:发布不起作用