我想使用其他父表的子表从profile表中获取数据。
这是表格
配置文件表
id
profile_name事务处理表
id
transaction_name子事务表
id
transaction_id
profile_id现在,我想使用急切加载从事务模型中获取配置文件名称。我尝试在事务模型中有一个虽然关系,但返回null
下面是我在事务模型中的关系
public function profileName()
{
return $this->hasOneThrough(
Profiler::class,
SubTransaction::class,
'profile_id',
'id',
'id',
'transaction_id',
);
}这就是我试图从事务控制器获取的内容
$options = Transaction::with([
'profileName:id,profile_name as profileName',
])
->get();发布于 2021-10-04 02:06:04
它返回null,因为我认为您在外键和本地键之间的匹配方面有一点小问题。您可以尝试以下代码:
return $this->hasOneThrough(
Profiler::class, //Final model we wish to access
SubTransaction::class, //The name of the intermediate model
'transaction_id', //Foreign key on sub_transaction table
'id', //Foreign key on profile table
'id', //Local key on transaction table
'profile_id', //Local key on sub_transaction table
);如果你有什么问题,请告诉我。
https://stackoverflow.com/questions/69430145
复制相似问题