【发布时间】:2021-03-15 17:30:53
【问题描述】:
我有一个巨大的问题,我找不到解决方法。我正在使用 Laravel 构建一个电子商务应用程序,构建一个良好的产品过滤系统至关重要。我有 products 和 attributes 具有多对多关系的表。数据透视表product_attribute 有额外的value(如果attribute 是颜色,则value 将是red)列。当进入商店页面时,有一个带有过滤选项的侧边栏。我可以显示的唯一选项是 brand,因为它与产品表是一对多的关系。显示这些属性的正确方法是什么。正如我提到的,属性对于页面上的产品应该是动态的。不同类别的不同产品(自行车、衣服、球、台球桌)可能具有不同的属性。
public function show($slug){
// Get products with attributes for specic
$category = Category::with('products')->where('slug',$slug)->firstOrFail();
// Collect brand ids
$b_ids = array_filter($category->products->pluck('brand_id')->toArray());
//Get brands to show in the filtering options
$brands = Brand::whereIn('id',$b_ids)->get();
$attributes = ?
return view('front.shop', compact('category','brands', 'attributes'));
}
【问题讨论】:
标签: laravel many-to-many e-commerce