【发布时间】:2020-04-23 13:31:42
【问题描述】:
$products= DB::table('products')
->leftJoin('categoryables', 'categoryables.categoryable_id', '=', 'products.id')
->leftJoin('categories', 'categoryables.category_id', '=', 'categories.id')
->leftJoin('brands', 'products.brand_id', '=', 'brands.id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id') //'products.color_id' = "1,3,5,6"
->whereIn('categoryables.category_id', $categories)
->select('products.*','categories.path', 'categories.title', 'brands.name_brand',
DB::raw("GROUP_CONCAT(colors.name_color) as name_colors"),
DB::raw("GROUP_CONCAT(colors.img_color) as img_colors")
)
->groupBy('products.id','categories.path','categories.title')
->paginate(5);
我需要为产品表添加颜色。 我创建了两个表:产品和颜色。 在产品表中有一个值为“1,3,5,6”的字段“Color_id” - 这是颜色表中带有“id”的行。如何获得所有颜色的商品?
【问题讨论】:
-
为什么用leftjoin为什么不用model之间的关系
-
使用多对多?