为Sentinel中的每个种子用户添加激活(Laravel)

时间:2018-02-21 11:44:29

标签: php database laravel seeding sentinel

使用Laravel的数据库种子我正在添加10个随机用户,当我尝试使用这些用户登录时,Sentinel给出了一个错误,我必须首先激活该帐户,我现在要做的是(当我' m seeding)为每个被播种的用户激活,这是我的DatabaseSeeder:

public function run()
{
    factory(App\User::class, 10)->create()->each(function($user) {
        DB::table('activations')->insert([
            'user_id' => $user->user_id,
            'code' => str_random(24),
            'completed' => '1'
        ]);
    });
}

我得到的问题是数据库正在填充随机用户和10次激活,但所有用户ID都是0

这里是用户工厂:

$factory->define(App\User::class, function (Faker $faker) {
    static $password;

    return [
        'id' => random_int(100000, 999999),
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
    ];
});

1 个答案:

答案 0 :(得分:0)

为激活添加工厂:

$factory->define(App\Activation::class, function (Faker $faker) {
    return [
        'code' => str_random(24),
        'completed' => '1'
    ];
});

并在用户模型中添加关系:

public function activation()
{
    return $this->hasOne('App\Activation');
}

然后你可以这样做:

factory(App\User::class, 10)->create()->each(function($user) {
    $user->activation()->save(factory(App\Activation::class)->make());
});