[英]Laravel 5.4 Auth::User() with relationship not working
我有Table Vendors(用于测试Auth关系)和Table Vendor_users,
但是我使用Auth::user()
它不是关系
和这个数据库
在供应商模型中
protected $table = 'vendor_users';
public function Vendor_test(){
return $this->belongsTo(Vendor_test::class);
}
和Vendor_test模型
protected $table = 'vendors';
public function Vendor(){
return $this->hasMany(Vendor::class);
}
从聊天和您当前的表结构中,您应该具有这样的关系
在供应商模型中
public function vendor_contact()
{
return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id');
}
在Vendor_contact模型中
protected $primaryKey = 'vendContactId'; //check this
public function vendor()
{
return $this->hasOne(Vendor::class, 'vendor_contact_id');
}
现在使用懒惰的渴望加载来加载vendor_contact
关系
Auth::user()->load('vendor_contact');
dd(Auth::user());
根据讨论内容和表结构,在模型vendor_users
添加关系函数。
protected $table = 'vendor_users';
public function vendor_contact()
{
return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id');
}
使用户与vendor_contact
并检查
$user = Auth::user()->with('vendor_contact')->first(); // As you asked with for auth
//OR
$user = Auth::user()->load('vendor_contact'); // From the rkj answer as I found this good.
// OR
$user = Vendor::find(1)->with('vendor_contact')->first();
dd($user);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.