Laravel 电子商务动态产品过滤
Laravel ecommerce dynamic product filtering
我遇到了一个大问题,找不到解决方法。我正在使用 Laravel 构建一个电子商务应用程序,为产品构建一个良好的过滤系统至关重要。我有 products
和 attributes
table 具有多对多关系。枢轴 table product_attribute
有额外的 value
(如果 attribute
是颜色,value
将是 红色 例如)柱子。当进入商店页面时,有一个带有过滤选项的侧边栏。我可以显示的唯一选项是 brand,因为它与产品 table 是一对多关系。显示这些属性的正确方法是什么。正如我提到的,属性对于页面上的产品应该是动态的。不同类别的不同产品(自行车、衣服、球、游泳池 tables)可能具有不同的属性。
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'));
}
预加载产品属性 - Category::with('products.attributes')
public function show($slug){
// Get products with attributes for specic
$category = Category::with('products.attributes')->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();
return view('front.shop', compact('category','brands'));
}
然后在视图中可以通过产品访问属性
that's not what I want. It just gives me the value. I want attribute name to be displayed at the top (for example COLOR) and values (Red, Blue, etc) at the bottom next to a checkbox for the user to filter
假设您将属性的所有可能值存储为选项
// ...
@foreach($categories as $category)
@foreach($category->products as $product)
@include('attribute-options', ['attributes' => $product->attributes])
@endforeach
@endforeach
// ...
将产品的属性显示提取到部分 - attribute-options.blade.php
@foreach($attributes as $attribute)
<h3>{{ $attribute->name }}
<ul>
@foreach($attribute->options as $option)
<li>
<label for="{{$attribute->name}}-{{$option}}">{{ $option }}
<input
type="radio"
value="{{$option}}"
id="{{$attribute->name}}-{{$option}}"
name="{{$attribute->name}}-{{$option}}"
/>
</label>
</li>
@endforeach
</ul>
@endforeach
我遇到了一个大问题,找不到解决方法。我正在使用 Laravel 构建一个电子商务应用程序,为产品构建一个良好的过滤系统至关重要。我有 products
和 attributes
table 具有多对多关系。枢轴 table product_attribute
有额外的 value
(如果 attribute
是颜色,value
将是 红色 例如)柱子。当进入商店页面时,有一个带有过滤选项的侧边栏。我可以显示的唯一选项是 brand,因为它与产品 table 是一对多关系。显示这些属性的正确方法是什么。正如我提到的,属性对于页面上的产品应该是动态的。不同类别的不同产品(自行车、衣服、球、游泳池 tables)可能具有不同的属性。
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'));
}
预加载产品属性 - Category::with('products.attributes')
public function show($slug){
// Get products with attributes for specic
$category = Category::with('products.attributes')->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();
return view('front.shop', compact('category','brands'));
}
然后在视图中可以通过产品访问属性
that's not what I want. It just gives me the value. I want attribute name to be displayed at the top (for example COLOR) and values (Red, Blue, etc) at the bottom next to a checkbox for the user to filter
假设您将属性的所有可能值存储为选项
// ...
@foreach($categories as $category)
@foreach($category->products as $product)
@include('attribute-options', ['attributes' => $product->attributes])
@endforeach
@endforeach
// ...
将产品的属性显示提取到部分 - attribute-options.blade.php
@foreach($attributes as $attribute)
<h3>{{ $attribute->name }}
<ul>
@foreach($attribute->options as $option)
<li>
<label for="{{$attribute->name}}-{{$option}}">{{ $option }}
<input
type="radio"
value="{{$option}}"
id="{{$attribute->name}}-{{$option}}"
name="{{$attribute->name}}-{{$option}}"
/>
</label>
</li>
@endforeach
</ul>
@endforeach