Laravel Eloquen模型加入
大家好!我在这里有一个场景:
应用表
-application_no
-record_id
要求人表
-req_id
-req_desc
要求checklist表
-req_id
-record_id
单个应用程序具有许多要求。现在,我想返回应用程序及其需求_checkList及其要求描述(
REQ_DESC来自需求表
)
示例输出应该是这样的:
[
{
"application_no": "201911213",
"record_id": '322',
"requirements": [
{
"req_id": 1,
"record_id": 322,
"status": 1,
"remarks": "",
"requirementDefinition": "Some requirement description"
},
{
"req_id": 2,
"record_id": 322,
"status": 1,
"remarks": "",
"requirementDefinition": "Some requirement description"
},
]
}
]
我能够生成类似的内容,但是我在regignerdementDefinition键
这是我的模型:
Application Model Model
public function requirements()
{
return $this->hasMany(RequirementChecklist::class, 'record_id');
}
EniglionementCheckList模型
public function requirementDefinition()
{
return $this->hasOne(Application::class, 'record_id');
}
和我的我的 模型查询:
$requirements = Application::where(['application.application_no' => $id])
->with('requirements.requirementDefinition')
->get();
我如何加入需求表以获取REQ_DESC吗?谢谢你!
Good day everyone! I have a scenario here:
Applications table
-application_no
-record_id
Requiremens table
-req_id
-req_desc
RequirementsChecklist table
-req_id
-record_id
A single application has many requirements. Now I want to return the application together with its requirement_checklist and its requirement description(
req_desc from Requirement table
)
The example output should be like this:
[
{
"application_no": "201911213",
"record_id": '322',
"requirements": [
{
"req_id": 1,
"record_id": 322,
"status": 1,
"remarks": "",
"requirementDefinition": "Some requirement description"
},
{
"req_id": 2,
"record_id": 322,
"status": 1,
"remarks": "",
"requirementDefinition": "Some requirement description"
},
]
}
]
I was able to produce something like that, but I'm getting null on the requirementDefinition key
Here are my Model:
Application Model
public function requirements()
{
return $this->hasMany(RequirementChecklist::class, 'record_id');
}
RequirementChecklist Model
public function requirementDefinition()
{
return $this->hasOne(Application::class, 'record_id');
}
And my query:
$requirements = Application::where(['application.application_no' => $id])
->with('requirements.requirementDefinition')
->get();
Any idea on how I can join the Requirement table to get the req_desc? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论