Laravel 5.1 - 模型工厂错误种子

时间:2016-06-16 12:19:14

标签: php laravel laravel-5.1 laravel-seeding

您好我正在尝试使用一些文章,BlogCategories,用户填充我的数据库。

当我做“php artisan db:seed”时,我有这个错误:

  

[ErrorException]参​​数2传递给   Illuminate \ Database \ Eloquent \ Factory :: define()必须是可调用的。   给出的字符串,调用   第22行的C:\ xampp \ htdocs \ 2016 \ database \ factories \ ModelFactory.php   和定义

我在我的模型中插入了所有关系,我完成了我的迁移!

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(管理员)是一个bolean字段。

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();
    }

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

我相信您的前两种方法意味着使用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),
    ];
});

答案 1 :(得分:0)

你需要像这样重新定义你的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();

答案 2 :(得分:-2)

而不是

$ factory-> define(dixard \ User :: class,' admin',function(Faker \ Generator $ faker)

只需删除"生成器"对于每种方法,它应该是这样的:

$ factory-> define(dixard \ User :: class,' admin',function(Faker $ faker)