2016-07-24 80 views
1

我需要在同一時間創建兩個用戶。這是我在AuthController中的代碼:Laravel多創建用戶

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

問題是令牌只存儲在最後創建的用戶中。在mysql數據庫中,對於第一個用戶,列remember_tokennull

如何創建兩個不同的標記?
如果您有另一個同時創建兩個用戶的好主意,請告訴我。

感謝

回答

0

儘量節省用戶是這樣的:

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

您的用戶模型申報$可填寫的屬性:

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

您也可以使用:

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

而在用戶模式:

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

或者你也可以做這樣的事情:

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