Laravel Eloquen模型加入

发布于 2025-01-21 07:29:50 字数 1680 浏览 0 评论 0原文

大家好!我在这里有一个场景:

应用表

-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文