2017-07-25 49 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:

相关问题