首页  >  问答  >  正文

如何在 Laravel 中通过属性(颜色、尺寸等)过滤产品

<p>我是 Laravel 新手,想要过滤掉特定产品。</p> <p>我的数据库中有两个表,第一个是表<code>products</code>,第二个是表<code>attributes</code>。</p> <p><strong>产品表</strong></p> <pre class="brush:php;toolbar:false;">Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->BigInteger('category_id')->unsigned()->nullable(); $table->string('name'); $table->string('code'); $table->integer('status')->default(1); $table->integer('featured')->default(1); $table->string('image'); $table->longText('short_description'); $table->longText('long_description'); $table->timestamps(); })</pre> <p><strong>产品属性表</strong></p> <pre class="brush:php;toolbar:false;">Schema::create('product_attributes', function (Blueprint $table) {. $table->bigIncrements('id'); $table->unsignedBigInteger('product_id'); $table->string('sku'); $table->string('size'); $table->string('color'); $table->string('price'); $table->string('stock'); $table->timestamps(); })</pre> <p><strong>关系</strong></p> <p>因为我有单一产品的多个属性</p> <pre class="brush:php;toolbar:false;">class Product extends Model { use HasFactory; public function attributes() { return $this->hasmany('App\Models\ProductAttributes', 'product_id'); } }</pre> <p><strong>我的刀片文件</strong></p> <pre class="brush:php;toolbar:false;"><form action="{{url('/product/filter')}}" method="post"> @csrf <input type="hidden"value="{{$slug}}"name="slug"> <div class="custom-control custom-checkbox d-flex align-items-center justify-content-between mb-3"> <input name="color" onchange="javascript:this.form.submit();" type="radio" class="custom-control-input" id="black" value="black"> <label class="custom-control-label" for="black">Black</label> </div> </form></pre> <p>我的控制器中有一个函数</p> <pre class="brush:php;toolbar:false;">public function shop() { $filter_products = Product::with('attributes')->where(['category_id' => $category->id, 'color' => $request->color]); return view('frontend.shop', compact('filter_products')); }</pre> <p>应用此函数后我没有得到任何结果</p> <p>请指导我如何根据特定尺寸或颜色在前端商店页面过滤产品。 以及商店功能中将包含哪些代码。</p> <p>请回复我,我将非常感谢你</p>
P粉821274260P粉821274260391 天前538

全部回复(2)我来回复

  • P粉908138620

    P粉9081386202023-08-28 15:59:41

    雷雷

    回复
    0
  • P粉033429162

    P粉0334291622023-08-28 13:52:36

    您需要按关系进行过滤,请查看文档

    https://laravel.com/docs/9 .x/eloquent-relationships#querying-relationship-existence

    举例说明 使用WhereHas

    $filter_products = Product::with('attributes')
       ->where(['category_id' => $category->id])
       ->whereHas('attributes',function($query) use($request){
          $query->where(['color' => $request->color]);
       });

    如果 with 中没有应用任何地方,则将返回所有属性

    您可以使用在 whereHas 中应用的相同过滤器来防止这种行为

    $filter_products = Product::with(['attributes'=>function($query) use($request){
            $query->where(['color' => $request->color]);
        }])
        ->where(['category_id' => $category->id])
        ->whereHas('attributes',function($query) use($request){
            $query->where(['color' => $request->color]);
        });

    回复
    0
  • 取消回复