2016-06-07 51 views
1

我使用PHP,Laravel 5.2和MySQL。用laravel中的外鍵保存對象

在用戶註冊期間,我需要創建一個新的Patient。但是,患者具有用戶ID,聯繫人ID和監護人ID(外鍵)。

當我試圖保存()的病人,我得到以下異常:

SQLSTATE [42S22]:列未找到:在 '字段列表'(SQL 1054未知列 'patient_id':更新users設置patient_id = 0,updated_at = 2016年6月7日12時59分35秒,其中id = 6)

的問題是,我沒有patient_id柱。相反,我有patientId

我不知道如何解決這個問題。任何幫助將不勝感激。如果這很重要,我可以包含遷移文件。

UserController.php

public function postSignUp(Request $request) 
{ 
    $this->validate($request,[ 
     'email' => 'required|email|unique:users', 
     'name' => 'required|max:100', 
     'password' => 'required|min:6' 
    ]); 


    $guardian = new Guardian(); 
    $guardian->guardianId = Uuid::generate();; 
    $guardian->save(); 

    $contact = new Contact(); 
    $contact->contactId = Uuid::generate(); 
    $contact->save(); 

    $user = new User(); 
    $user->email = $request['email']; 
    $user->name = $request['name']; 
    $user->password = bcrypt($request['password']); 
    $user->save(); 


    $patient = new Patient(); 
    $patient->patientId = (string)Uuid::generate(); 
    $patient->user()->save($user); 
    $patient->contact()->save($contact); 
    $patient->guardian()->save(guardian); 



    $patient->save(); 
    Auth::login($user); 

//  return redirect()->route('dashboard'); 
} 

Patient.php

class Patient extends Model 
{ 
    protected $primaryKey='patientId'; 
    public $incrementing = 'false'; 
    public $timestamps = true; 

    public function user() 
    { 
     return $this->hasOne('App\User'); 
    } 
    public function contact() 
    { 
     return $this->hasOne('App\Contact'); 
    } 
    public function guardian() 
    { 
     return $this->hasOne('App\Guardian'); 
    } 
    public function allergies() 
    { 
     return $this->belongsToMany('App\PatientToAllergyAlert'); 
    } 
    public function medicalAlerts() 
    { 
     return $this->belongsToMany('App\PatientToMedicalAlert'); 
    } 
} 

user.php的

class User extends Authenticatable 
{ 
    protected $fillable = [ 
     'name', 'email', 'password', 
    ]; 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 
    public function patient() 
    { 
     return $this->belongsTo('App\Patient'); 
    } 
} 

Contact.php

class Contact extends Model 
{ 
    protected $table = 'contacts'; 
    protected $primaryKey = 'contactId'; 
    public $timestamps = true; 
    public $incrementing = 'false'; 

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

Guardian.php

class Guardian extends Model 
{ 
    protected $table = 'guardians'; 
    protected $primaryKey = 'guardianId'; 
    public $timestamps = true; 
    public $incrementing = 'false'; 

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

回答

4

您沒有正確定義的關係。首先,將表格字段填入Patient,Contact,Guardian類中的$ fillable數組(就像在User類中一樣)。

如果您想要使用患者與用戶之間的hasOne關係,您需要在患者表上使用user_id字段。您也可以使用belongsTo關係。

如果你想使用自定義的列名,只需指定他們的關係方法:

public function user() 
{ 
    return $this->hasOne('App\User', 'id', 'user_id'); 
    // alternatively 
    return $this->belongsTo('App\User', 'user_id', 'id'); 
} 

只需經過文件沒有跳過的段落,你會在幾分鐘內走了:) https://laravel.com/docs/5.1/eloquent-relationships#defining-relationships

而且,這是不行的:

$patient = new Patient(); 
$patient->patientId = (string)Uuid::generate(); 
$patient->user()->save($user); 

新的病人()只創建對象,但並不保存在DB中,所以你將無法保存關係。您需要創建對象,並將其存儲到數據庫,以避免這個問題:

$patient = Patient::create(['patientId' => (string)Uuid::generate()]); 
$patient->user()->save($user); 
... 

// or 
$patient = new Patient(); 
$patient->patientId = (string)Uuid::generate(); 
$patient->save(); 
$patient->user()->save($user); 
... 
+0

感謝您的時間...我會盡力做出改變 – dimitrisdan

0

當你設置你的關係,你可以指定在其他模型中的主鍵的名稱。看看here

我不確定,但我認爲你的關係沒有正確定義。