php - Laravel 5.4 Auth::User() 与关系不起作用
问题描述
我有 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());
推荐阅读
- react-native - React Native 获取 Firebase 数据库推送令牌密钥
- python-3.x - AttributeError 尝试在 Python 中使用 Selenium 自动化 WhatsApp
- c++ - 我可以分配一个指针对象数组=另一个对象吗?
- python - 正则表达式跨越多行
- python - ElementNotInteractableException:消息:元素不可交互:元素当前不可见,可能无法使用机器人框架进行操作
- python - 根据索引合并两个系列
- sql-server - 提高 SQL Server 中触发器的性能
- azure - 组名称作为 Azure AD 中的声明
- ios - 使用 appstore 验证应用程序时出现代码登录问题
- django - 无法在 Django 网站上转发或跳过视频