Laravel 5.1 - 模型工厂错误种子

Laravel 5.1 - Model Factory Error seed

您好,我正在尝试用一些文章、博客类别、用户填充我的数据库。

当我 "php artisan db:seed" 我有这个错误:

[ErrorException] Argument 2 passed to Illuminate\Database\Eloquent\Factory::define() must be callable. string given, called in C:\xampp\htdocs16\database\factories\ModelFactory.php on line 22 and defined

我在我的模型中插入了所有关系,我的迁移工作做得很好!

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 can be 0 (customer), 1(artist), 3(admin) is a bolean field.

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

感谢您的帮助!

我相信您打算在前两种方法中使用 defineAs() 而不是 define()

https://laravel.com/docs/5.1/testing § "Multiple Factory Types"

$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),
    ];
});

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

而不是

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

只需删除每个方法的 "Generator",它应该是这样的:

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