0

我已经定义了三个级别的类别。主要、次要和产品类别。

我在primary_categories表下有两个类别,即女装和男装。

secondary_categories我下面有传统服装(女性)、鞋类(女性)、西方(女性)、西方服装(男性)、鞋类(男性)、裤子(男性)等类别。

最后,product_categories我有裤子、T 恤、库尔塔、凉鞋等类别。

在为产品保存类别时,我products在列中使用了表category_id

现在我想获得属于女性时尚的产品。我怎样才能做到这一点?

主要类别

public function up()
{
    Schema::create('primary_categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });
}

次要类别

public function up()
{
    Schema::create('secondary_categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('slug');
        $table->unsignedBigInteger('primary_category_id')->nullable();
        $table->foreign('primary_category_id')->references('id')->on('primary_categories')->onDelete('SET NULL');
        $table->timestamps();
    });
}

最终类别

public function up()
{
    Schema::create('product_categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('slug')->unique();
        $table->unsignedBigInteger('secondary_category_id')->nullable(); 
        $table->foreign('secondary_category_id')->references('id')->on('secondary_categories')->onDelete('SET NULL');
        $table->timestamps();
    });
}

在添加产品时,category_idproduct_categories 进入产品表中。

4

1 回答 1

2

主要类别模型

public function secondaryCategories(){
   return $this->hasMany(App\SecondaryCategory::class, 'primary_category_id', 'id');
}

次要类别模型

public function primaryCategory(){
    return $this->belongsTo(App\PrimaryCategory::class, 'primary_category_id', 'id');
}

public function productCategories(){
   return $this->hasMany(App\ProductCategory::class, 'secondary_category_id', 'id');
}

产品类别模型

public function secondaryCategory(){
   return $this->belongsTo(App\SecondaryCategory::class, 'secondary_category_id', 'id');
}

public function products(){
    return $this->hasMany(App\Product::class, 'category_id', 'id');
}

产品型号

public function productCategory(){
      return $this->belongsTo(App\ProductCategory, 'category_id', 'id');
}

控制器

获取具有给定 PrimaryCategory 的所有产品

选项1:数据库查询,几种收集方法。

$name = "Women's Fashion";

$pc = PrimaryCategory::with(
        'secondaryCategories.productCategories.products')
        ->where('name', $name)->first();

$products = $pc->secondaryCategories->pluck('productCategories')
               ->collapse()->pluck('products')->collapse();

 

或者

[带约束的嵌套预加载]

选项 2:数据库查询

$name = "Women's Fashion";

$products = Product::whereHas('productCategory', function($query) 
    use($name) {
         $query->whereHas('secondaryCategory', function($query) 
         use($name)  { 
             $query->whereHas('primaryCategory', function($query) 
             use($name){
                 $query->where('name', $name);
             });
         });
   })
   ->with([
    'productCategory' => function($query) use($name) {
         $query->whereHas('secondaryCategory', function($query) use($name)
          { 
             $query->whereHas('primaryCategory', function($query) 
               use($name){
                   $query->where('name', $name);
               });
          });
     },
    'productCategory.secondaryCategory'=> function($query) use($name)
     { 
             $query->whereHas('primaryCategory', function($query) 
               use($name){
                   $query->where('name', $name);
               });
     },
    'productCategory.secondaryCategory.primaryCategory' =>                   
      function($query) use($name) {
            $query->where('name', $name);
     }])->get();
于 2020-07-26T07:34:23.490 回答