Php 如何在Eloquent中从嵌套关系中选择特定字段

Php 如何在Eloquent中从嵌套关系中选择特定字段,php,laravel-5,eloquent,Php,Laravel 5,Eloquent,我有以下雄辩的疑问: public function firstSubsectionIdsOnly() { return $this->model->with(['sections' => function ($q) { $q->with(['subsections' => function ($q2) { $q2->first(); }

我有以下雄辩的疑问:

 public function firstSubsectionIdsOnly()
    {
        return $this->model->with(['sections' => function ($q) {
            $q->with(['subsections' => function ($q2) {
                $q2->first();

            }
            ])->first();
        }])->whereHas('sections.subsections')->first();
    }
这将返回如下内容:

{
    "id": 1,
    "name": "Training exercise",
    "entry": "<p>sss</p>",
    "created_at": "2018-04-20 09:38:36",
    "updated_at": "2018-04-20 10:08:27",
    "sections": [
        {
            "id": 1,
            "name": "Section 1 Training",
            "parent": null,
            "position": 1,
            "created_at": "2018-05-04 09:37:23",
            "updated_at": "2018-05-04 09:37:23",
            "pivot": {
                "training_exercise_id": 1,
                "section_id": 1
            },
            "subsections": [
                {
                    "id": 2,
                    "name": "Subsection 1 training",
                    "parent": 1,
                    "created_at": "2018-05-04 09:54:09",
                    "updated_at": "2018-05-04 09:54:09"
                }
            ]
        }
    ]
}
{
        "id": 1,
        "name": "Training exercise",
        "entry": "<p>sss</p>",
        "created_at": "2018-04-20 09:38:36",
        "updated_at": "2018-04-20 10:08:27",
        "sections": [
            {
                "id": 1,
                "subsections": [
                    {
                        "id": 2,
                    }
                ]
            }
        ]
    }
{
“id”:1,
“名称”:“训练演习”,
“条目”:“sss

”, “创建时间”:“2018-04-20 09:38:36”, “更新时间”:“2018-04-20 10:08:27”, “章节”:[ { “id”:1, “名称”:“第1节培训”, “父项”:空, "立场":一,, “创建时间”:“2018-05-04 09:37:23”, “更新时间”:“2018-05-04 09:37:23”, “支点”:{ “训练练习”id:1, “部门id”:1 }, “小节”:[ { “id”:2, “名称”:“第1节培训”, “家长”:1, “创建时间”:“2018-05-04 09:54:09”, “更新时间”:“2018-05-04 09:54:09” } ] } ] }
我希望它只为每个关系选择id字段,因此它应该返回如下内容:

{
    "id": 1,
    "name": "Training exercise",
    "entry": "<p>sss</p>",
    "created_at": "2018-04-20 09:38:36",
    "updated_at": "2018-04-20 10:08:27",
    "sections": [
        {
            "id": 1,
            "name": "Section 1 Training",
            "parent": null,
            "position": 1,
            "created_at": "2018-05-04 09:37:23",
            "updated_at": "2018-05-04 09:37:23",
            "pivot": {
                "training_exercise_id": 1,
                "section_id": 1
            },
            "subsections": [
                {
                    "id": 2,
                    "name": "Subsection 1 training",
                    "parent": 1,
                    "created_at": "2018-05-04 09:54:09",
                    "updated_at": "2018-05-04 09:54:09"
                }
            ]
        }
    ]
}
{
        "id": 1,
        "name": "Training exercise",
        "entry": "<p>sss</p>",
        "created_at": "2018-04-20 09:38:36",
        "updated_at": "2018-04-20 10:08:27",
        "sections": [
            {
                "id": 1,
                "subsections": [
                    {
                        "id": 2,
                    }
                ]
            }
        ]
    }
{
“id”:1,
“名称”:“训练演习”,
“条目”:“sss

”, “创建时间”:“2018-04-20 09:38:36”, “更新时间”:“2018-04-20 10:08:27”, “章节”:[ { “id”:1, “小节”:[ { “id”:2, } ] } ] }

我尝试添加
$q->select('id')
子部分
嵌套闭包,但返回空的
子部分
数组


你知道我怎样才能做到这一点吗?我使用的是Laravel 5.6,您必须包含外键列。对于
子节
关系:

$q2->select('id', 'parent');

正如其他人之前所述,您需要在选择的值中包含外键。假设我们有两个模型<代码>父项和子项。这是您必须选择的最小值

App\Parent::select('id')->with(['children' => function ($query) {
    $query->select('id', 'parent_id');
}])->get();
它会让你觉得

Illuminate\Database\Eloquent\Collection {
    all: [
        App\Parent {
            id: 1,
            children: Illuminate\Database\Eloquent\Collection {
                all: [
                    App\Child {
                        id: 109,
                        parent_id: 1,
                    },
                    App\Child {
                        id: 153,
                        parent_id: 1,
                    },
                ],
            },
        },
    ],
}
如果你想求逆,你仍然需要外键,就像这样

App\Child::select('id', 'parent_id')->with('parent' => function ($query) {
    $query->select('id');
}])->get();
这是你的网

Illuminate\Database\Eloquent\Collection {
    all: [
        App\Child {
            id: 1,
            parent_id: 58,
            parent: App\Parent {
                id: 58,
            },
        },
    ],
}
我正在使用
select()
而不是
get()
来查看查询的易读性,但是您必须使用
select()
来查看子查询


如果将
与('relation')=>函数($query){$query->get(['id']);}
一起使用,您将获得整个对象。

“我已尝试将
$q->select('id');
添加到嵌套闭包中。”。哪个嵌套闭包?有两个参数使用相同的参数。也许这就是问题所在?我在
小节
闭包中尝试过,还将参数名称更改为
$q2
。仍然为
子部分返回空数组
@Loek有什么想法吗?请您对答案给出反馈?