我有一个查询(使用查询构建器),如下所示:
$results = DB::table('owner')
->join('pet', 'pet.owner_id', '=', 'owner.id')
->select('owner.*', 'pet.name AS pet_name')
->where('owner.name', 'John')
->get();
return response()->json($results);
并且响应如下:
{
[
"id" = 1,
"name" = "John",
"gender" = "M",
"pet_name" = "Judy"
],
[
"id" = 1,
"name" = "John",
"gender" = "M",
"pet_name" = "Nick"
],
}
我只想要一个结果显示所有宠物的主人数据, 这将是:
{
[
"id" = 1,
"name" = "John",
"gender" = "M",
"pet_name" = ["Judy", "Nick"]
]
}
如何使用查询构建器执行此操作?