【问题标题】:Laravel eloquent with categoriesLaravel 雄辩的分类
【发布时间】:2020-01-14 01:32:26
【问题描述】:

我有一个关于 laravel eloquent 的问题。

数据库表

类别:id root_id 名称

产品 ID 名称等。

product_categories id product_id category_id

所以它可能是 CategoryA 有一个子 CategoryB 而 CategoryB 本身有一个子 CategoryC。

当我点击 CategoryA 时,我想查找属于 CategoryA、CategoryB、CategoryC 的所有产品

Category Model

   public function cats()
    {
        return $this->hasMany(Category::class);
    }


    public function childrenCategories()
    {
        return $this->hasMany(Category::class)->with('cats');
    }

产品型号

public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

控制器

//首先我得到所有类别的所有id,所有级别的子类别。

        $categories = Category::where('category_id',$category_id)->with('childrenCategories')->get();
        $all_cat_array=array();$all_prod=array();

foreach ($categories as $category)
{
    foreach ($category->childrenCategories as $childCategory) {
        array_push($all_cat_array, $childCategory->id);
    }
    array_push($all_cat_array,$category->id);

}

//然后我得到所有产品的id

foreach ($all_cat_array as $cat)
{
    if(CategoryProduct::where('category_id',$cat)->exists()) {
        $prod=CategoryProduct::where('category_id',$cat)->pluck('product_id');
        array_push($all_prod,$prod );
    }
}

但我不想使用所有这些 foreach,因为我想优化代码。 我该怎么做才能让它更简单???

【问题讨论】:

  • CategoryA 与 CategoryB 有关系。是一对多的关系吗?
  • 是的。就像:CategoryA whth id 1 没有 root_id,另一方面 CateogryB 的 root_id 是 1

标签: laravel eloquent


【解决方案1】:

我建议您在类别模型中使用嵌套集合结构。

在这个包https://github.com/lazychaser/laravel-nestedset中很好的实现了。

您可以通过 2 个查询获得类别产品及其所有后代(无限嵌套)。

如果 Category 属于ToMany 产品:

$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');

$products = Product::whereHas('categories', function ($query) use ($categoryIds) {
    $query->whereIn('categories.id', $categoryIds);
});

如果类别有很多产品

$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');

$products = Product::whereIn('category_id', $categoryIds);

【讨论】:

    【解决方案2】:

    更多信息请阅读Laravel Docs

    数据库

    A 类数据库

    Schema::create('category_as', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->unsignedBigInteger('product_id');
      ...
      $table->timestamps();
    
      $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    });
    

    B 类数据库

    Schema::create('category_bs', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->unsignedBigInteger('category_a_id');
      ...
      $table->timestamps();
    
      $table->foreign('category_a_id')->references('id')->on('category_as')->onDelete('cascade');
    });
    

    型号

    产品型号

    public function categories_a() {
      return $this->hasmany(Category_a::class);
    }
    

    A 类模型

    public function categories_b() {
      return $this->hasmany(Category_b::class);
    }
    
    public function product() {
      return $this->belongsTo(Product::class);
    }
    

    B 类型号

    public function category_a() {
      return $this->belongsTo(Category_a::class);
    }
    
    public function clients() {
       return $this->hasManyThrough(Client::class, Code::class);
    }
    

    控制器

    public function index() {
      $products = Product::all();
      $query = $products->categories_a->categories_b;
      dd($query);
    }
    

    已编辑

    数据库

    A 类数据库

    Schema::create('categories', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->unsignedBigInteger('product_id');
      ...
      $table->timestamps();
    
      $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    });
    

    型号

    产品型号

    public function categories() {
      return $this->hasmany(Category::class);
    }
    

    类别模型

    public function product() {
      return $this->belongsTo(Product::class);
    }
    

    控制器

    public function index() {
      $products = Product::all();
      $query = $products->categories;
      dd($query);
    }
    

    【讨论】:

    • 感谢您的回复。但我只有一张名为类别的表。这意味着我只有一个模型。这种情况我该怎么办?
    • 嘿,有解决办法吗?
    • 请具体说明你的问题,告诉我你想要什么
    • 再次感谢您,但您仍然没有理解我:/ 这是多对多的关系。我还有一个表 category_product。我想显示一个类别的所有产品(它有子类别,子类别本身也有子类别等等(一个永无止境的循环))
    • 嘿,我找到了办法,但我需要你的帮助来优化代码...你能帮帮我吗?提前致谢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2023-03-11
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多