简体   繁体   English

Laravel多创建用户

[英]Laravel multi creation user

I need to create two users in the same time. 我需要同时创建两个用户。 Here is my code in the AuthController: 这是我在AuthController中的代码:

protected function create(array $data) {

    $profile = New \App\ClientDetail();
    $profile->s_firstname = $data['s_firstname'];
    $profile->s_surname = $data['s_surname'];
    $profile->p_firstname = $data['p_firstname'];
    $profile->p_surname = $data['p_surname'];
    $profile->s_mobile_number = $data['s_mob_number'];
    $profile->s_home_number = $data['s_home_number'];
    $profile->p_mobile_number = $data['p_mob_number'];
    $profile->p_home_number = $data['p_home_number'];
    [...all the form information fields]
    $profile->save();
   /*FIRST USER TO CREATE*/ 
    if($data['s_email']) {
        $user = User::create([
            'detail_id' => $profile->id,
            'email' => $data['s_email'],
            'password' => bcrypt('secret'),
        ]);
        $user->attachRole($student);
    }
   /*SECOND USER TO CREATE*/
    if($data['p_email']) {
        $user = User::create([
            'detail_id' => $profile->id,
            'email' => $data['p_email'],
            'password' => bcrypt('secret'),
        ]);
        $user->attachRole($parent);
    }

    return $user;
}

The problem is that the token is stored just for the last created user. 问题是令牌仅为最后创建的用户存储。 In the mysql database, for the first user the column remember_token is null . 在mysql数据库中,对于第一个用户,Remember_token remember_token null

How is it possible to create two different tokens? 如何创建两个不同的令牌?
If you have an other best idea for creating two users at the same time, please let me know. 如果您还有另一个最好的想法可以同时创建两个用户,请告诉我。

Thanks 谢谢

Try saving the users like this: 尝试像这样保存用户:

$user = new User($data);
$user->save();

You have to declare the $fillable property in your User model: 您必须在用户模型中声明$ fillable属性:

protected $fillable = ['email','password', 'detail_id'];

Also you can use: 您也可以使用:

$user->profile()->associate($profile->id);

And in the User model: 在用户模型中:

public function profile()
{
    return $this->belongsTo('App\Profile');
}

Or you can do something like this: 或者,您可以执行以下操作:

$profile->users()->createMany($usersData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM