我如何使用Laravel中的联接查询来组合这些字段

时间:2018-10-15 14:46:41

标签: mysql laravel laravel-5 eloquent

我正在构建一个API。我有2个表“产品”和“产品到类别”映射。 产品具有多个类别。我不使用模型。

DB::table('products_to_categories')
    ->LeftJoin('products', 'products.products_id', '=', 'products_to_categories.products_id')
    ->LeftJoin('categories_description','categories_description.categories_id','=','products_to_categories.categories_id')
    ->leftJoin('manufacturers','manufacturers.manufacturers_id','=','products.manufacturers_id')
    ->leftJoin('manufacturers_info','manufacturers.manufacturers_id','=','manufacturers_info.manufacturers_id')

当前我正在获取输出

{
    "products_id": 12,
     "categories_id": 1,
     "categories_description_id": 1,
     "categories_name": "Animal Figures",
},
{
     "products_id": 12,
     "categories_id": 2,
     "categories_description_id": 4,
     "categories_name": "Arts Crafts",
}

我想将两者结合起来并得到

的输出
{
     "products_id": 12,
     "categories_id": [1,2],
     "categories_description_id": [1,4],
     "categories_name": ["Animal Figures","Arts Crafts"]
}

0 个答案:

没有答案