带约束的嵌套预加载:从父类别中获取所有产品的产品。类别下定义了三个不同table

NESTED EAGER LOADING WITH CONSTRAINTS: Getting products all products from the parent category. Category are defined under three different table

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

我在 primary_categories table 下有两个类别,分别是女装和男装。

secondary_categories 下,我有传统服饰(女士)、鞋类(女士)、西部服饰(女士)、西部服饰(男士)、鞋类(男士)、裤子(女士)等类别男性)等等。

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

在为产品保存类别时,我在 category_id 栏中使用了 products table。

现在我想购买属于女性时尚的产品。我该怎么做?

主要类别

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();
    });
}

添加产品时,product_categories 的 category_id 进入产品 table。

主要类别模型

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 的所有产品

方案一:DB查询,几种采集方式。

$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();