我试图获取与特定列表相关的所有id,但我的查询只是返回表中的第一个相关id,而不是其他的。
表
List | id | person_id | name | description
| 1 | 10 | Test List | null
List_Ref | id | list_id | data_id
| 1 | 1 | 100
| 2 | 1 | 101查询
$lists = DB::table('List')
->leftJoin('List_Ref', 'List_Ref.list.id', '=', 'List.id')
->select('List.id', 'List.name', 'List_Ref.data_id')
->where('person_id', Auth::user()->person_id)
->groupBy('List.id')
->orderBy('List.id')
->get();结果(拉幅模和转储)
#items: array:1 [
0 => {
"id" : 1
"name" : "Test List"
"data_id" " 100
}
]我想得出如下的结果
#items: array:1 [
0 => {
"id" : 1
"name" : "Test List"
"data_id" => {
"id" : 100
"id" : 101
}
}
]发布于 2016-10-04 06:01:43
如果希望获得可用的分组项,请在构建查询时不要使用组。
就像这样:
$lists = DB::table('List')
->leftJoin('List_Ref', 'List_Ref.list.id', '=', 'List.id')
->select('List.id', 'List.name', 'List_Ref.data_id')
->where('person_id', Auth::user()->person_id)
->get();
$lists->groupBy('List.id')->orderBy('List.id');使用laravel集合的groupBy和orderBy方法获得分组项。希望它能帮到你!
发布于 2016-10-03 06:37:58
我不知道拉拉,但你正在做一个从“列表”到"List_Ref“的左联接。因此,Talbe中的每个条目都将与List_Ref的一个条目匹配。
试着加入。
发布于 2016-10-03 07:16:47
不要使用groupBy('List.id'),它将根据列表表的id对数据进行分组&始终只返回单个数据。
https://stackoverflow.com/questions/39825681
复制相似问题