Laravel 5.2将每个结果中的某个值绑定到数组中

时间:2016-08-02 09:32:14

标签: php mysql laravel-5.2 query-builder

我有一个查询(使用查询构建器),如下所示:

$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"]
 ]
}

如何使用查询构建器执行此操作?

0 个答案:

没有答案