2017-05-26 46 views
0

在Laravel的文檔,檢索驗證的用戶:如何與其他表驗證::用戶

$user = Auth::user(); 

這是我的表的模式(例如)

tblUser 
    id // 1 
    userid // admin 
    password // 12345 

tblUserInfo 
    id // 1 
    userid // admin 
    first_name // Roi 

是否有可能將tblUserInfo與經過身份驗證的用戶聯繫起來?例如像

Auth::user()->with('userinfo')->first_name;

或者是有沒有解決辦法得到驗證的用戶將FIRST_NAME我打的視圖之前?

+0

閱讀laravel這裏更關係 https://laravel.com/docs/5.4/eloquent-relationships –

+0

你應該問我,而不是笑 – Demonyowh

+0

大聲笑跆拳道,我PMEDü在Facebook上你永遠回覆 – Roi

回答

3

你應該能夠只是做

$firstname = Auth::user()->userinfo->first_name; 

這裏假設你有正確定義的關係。第二和第三個參數,因此 - 沿着

class User extends Authenticatable 
{ 
    // ... 

    public function userinfo() 
    { 
     return $this->hasOne(UserInfo::class); 
    } 
} 

鑑於你的非標準表模式,你可能需要明確在hasOne()方法指定的外國和本地鍵列名線的東西。

return $this->hasOne(UserInfo::class, 'id', 'id'); // or 'userid' depending on what you're using as a FK 
              ^^^^^^^^^ 
+0

謝謝,我之前嘗試過,並沒有工作,但現在它的工作 – Roi

相關問題