douchao0358 2016-06-16 12:19
浏览 71
已采纳

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!

  • 写回答

3条回答 默认 最新

  • douliao1911 2016-06-16 12:35
    关注

    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),
        ];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀