如何使用外键自定义Auth :: user()列?拉拉韦尔 [英] How to customize Auth::user() columns with foreign key? Laravel
问题描述
首先,每次我们登录laravel时,默认设置是始终可以通过使用 Auth :: user()来访问用户(表).
Firstly, everytime we login in laravel, the default is that the user(table) is always accessible by just using Auth::user().
请参阅以下2个表格:
我的奋斗是,如何使用用户表中的subscriber_id作为外键将订户的列添加到用户(表)中.
See 2 tables below:
My struggle is, how can I add subscriber's columns to users(table) using subscriber_id from user table as foreign key.
我的期望是,我希望所有这些(以下内容)在登录后都可以在全球范围内访问.
My expectation is, I want all these(below) to be globally accessible after I logged in.
Auth::user()->name
Auth::user()->age
Auth::user()->gender
Auth::user()->marital_status
Auth::user()->subscriber_id
Auth::user()->subscriber_name
Auth::user()->subscriber_status
Auth::user()->subscriber_created_at
Auth::user()->subscriber_created_at
我一直在Google上寻找答案,但没有帮助.我希望有人可以帮助我.
I been searching for an answer on google but none helps. I wish someone could help me to this.
推荐答案
请参见 https://laravel.com/docs/5.7/eloquent-relationships
在用户"模型上设置关系.
Set the relationship on Users model.
示例,对于一对一关系:
Example, for a one to one relation:
public function subscriber()
{
return $this->hasOne('App\Subscribers', 'id', 'subscriber_id');
}
这篇关于如何使用外键自定义Auth :: user()列?拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!