【发布时间】:2022-01-10 20:28:06
【问题描述】:
这是我的桌子的样子
| departments | course | subjects |
|---|---|---|
| id | id | id |
| head | department_id | course_id |
| course_name | subject_code |
在我的模型中,我就是这样做的,但似乎不起作用,我不知道为什么:
public function departments()
{
return $this->hasOneThrough(
subjectlist::class,
department::class,
'id',
'course_id',
'id',
'id'
);
}
我尝试使用hasOneThrough时的输出:
{
"id": 1,
"department_name": "Business",
"head": "John smith",
"email": "smith@gmail.com",
"contact_number": "09300282103",
"created_at": null,
"updated_at": null,
"subject_list": {
"id": 1,
"course_id": 1,
"subject_code": "Math1",
"description": "math test",
"year_type": 1,
"price": "200.00",
"units": "5.00",
"created_at": null,
"updated_at": null,
"laravel_through_key": 1
}
}
我不希望输出应该是这样的,请帮助我如何做到这一点:
{
"id": 1,
"department_name": "Business",
"head": "John smith",
"email": "smith@gmail.com",
"contact_number": "09300282103",
"created_at": null,
"updated_at": null,
"subject_list": {
"id": 1,
"course_id": 1,
"subject_code": "Math1",
"description": "math test",
"year_type": 1,
"price": "200.00",
"units": "5.00",
"created_at": null,
"updated_at": null,
"laravel_through_key": 1
},
"department": {
"id": 1,
"department_name": "ComputerScience"
}
}
【问题讨论】:
-
请出示您的型号查询码。
标签: javascript php laravel model relationship