我正在使用Laravel 5.1获取一组相关问题的分类。
我只想要嵌套问题的ID,但为了让select
起作用,我需要指定连接列,也就是category_id
:
$categories = Category::with(['questions'=>function($query){
$query->select('id', 'category_id');
}])->get();
这让我感到:
[
{
"id": 1,
"category": "Anatomy",
"created_at": "2016-04-13 00:46:12",
"updated_at": "2016-04-13 00:46:12",
"questions":[
{"id": 1, "category_id": 1},
{"id": 2, "category_id": 1},
{"id": 3, "category_id": 1},
{"id": 4, "category_id": 1},
...
]
}, ...
]
我该如何将
questions : [
中的内容仅限于ID而非对象,使其变为:
[
{
"id": 1,
"category": "Anatomy",
"created_at": "2016-04-13 00:46:12",
"updated_at": "2016-04-13 00:46:12",
"questions":[
1, 2, 3, 4 ...
]
}, ...
]
我尝试使用map和list收集ID,但这样做不起作用:
我尝试使用map和list收集ID。这种方法行不通:
$test = collect($categories)->map(function ($q) {
return collect($q)->lists('id')->toArray();
});
我希望在这里处理它,这样我就不需要在前端处理它。