【发布时间】: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 方法中的第二个参数是什么?
标签: php laravel laravel-5.1 laravel-seeding