Laravel 5.1 - 模型工厂错误种子

Hi i'm trying to populate my database with some Articles, BlogCategories, Users.

when i do "php artisan db:seed" i have this error:

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

I inserted all relations in my models and i done my migrations good!

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

Thank you for your help!

I believe you meant to use defineAs() instead of define() for your first two methods:

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

you need to redefine your ModelFactory like this.

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

then in your DatabaseSeeder file :

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

Instead of

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

just remove the "Generator" for each method and it should be like this:

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