我有表供应商(用于测试身份验证关系)和表 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());
关于php - Laravel 5.4 Auth::User() 关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256855/