2017-07-25 48 views
0

我有一個Laravel項目,我需要登錄存儲在另一個表中的用戶信息。獲取存儲在另一個表(Laravel)中的用戶信息

我試過使用雄辯,但沒有運氣。這是一個例子。

我有一個User模型連接到Login表:

public function LoginInfo() 
{ 
    return $this->belongsTo('App\LoginInfoModel', 'infoID'); 
} 

而且一LoginInfoModel,與此相聯繫:

public function User() 
{ 
    return $this->hasOne('App\User','UserID'); 
} 

但是通過輸出Auth::user(),有一個從LoginInfo表中沒有任何信息。

任何建議,也許這可以做不使用雄辯?

感謝

+0

我發現我的錯誤,我沒有得到兩個模型之間的建立關係,我應該提到第二個表的外鍵,而不是他的一個關鍵。這是問題。 –

回答

0

Auth::user()僅僅是用戶模型的實例,因此,如果你有LOGINFO關係上的用戶模型,你可以簡單地做:

Auth::user()->LoginInfo() 
+0

我有一個錯誤:類Illuminate \ Database \ Eloquent \ Relations \ BelongsTo的對象無法轉換爲字符串 –

0

如果你調用用戶的關係對象您將檢索該信息。

Auth::user()->LoginInfo

正如寫在docs

The first argument passed to the hasOne method is the name of the related model. Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship methods as if they were properties defined on the model:

相關問題