首页 文章

根据产品列出parentcategory下的子类别

提问于
浏览
-1

如 Headers 中所述,问题是如何根据产品列出父类别下的子类别 . 我没有问题列出父类别及其所有子类别,正如我在本文末尾的示例代码中所示 . 但我需要根据产品列出子类别 .

这是我的类别的示例结构:

电子
-电脑
-电话
-Gadget

杂货
-餐饮
-饮料

This is my products table migration:

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->decimal('price')->nullable();
        $table->timestamps();
    });

This is my categories table migration

Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('parent_id')->nullable();
        $table->string('name')->nullable();
        $table->string('description')->nullable();
        $table->timestamps();
    });

这一个是 category_product ,它在 categoryproduct 之间用作 many to many 表:

Schema::create('category_product', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('product_id')->unsigned()->index();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->integer('category_id')->unsigned()->index();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });

我已经 Build 了所有的关系 . 这是我的模特:

这是我的分类模型:

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [
        'name',
    ];
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }

    public function parent()
    {
        return $this->belongsTo('App\Category', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('App\Category', 'parent_id');
    }

}

这是我的产品型号:

class Product extends Model
{
   public function categories()
   {
     return $this->belongsToMany('App\Category');
   }
}

这是我的ProductController.php,我可以使用以下代码显示所有父类别及其子类别:

public function show($id)
 {   
      $product = Product::findOrFail($id); 
      $categories = Category::with('children')->get();
      return view('products.show', compact('product','categories'));

 }

所以我的product.shows.blade看起来像这样:

@foreach($categories as $item)
    @if($item->children->count() > 0 )
        <li>
            {{ $item->name }}
            <ul>
                @foreach($item->children as $submenu)
                    <li>{{ $submenu->name }}</li>
                @endforeach
            </ul>
        </li>
    @endif

@endforeach

//输出:

Electronic  
 Computer
 Phone
 Gadget

Grocery
 Food
 Drinks

但是,假设这个特定产品(称为产品1)的父类别为 Electronic ,子类别为 ComputerPhone ,我已将它们附加到数据库中 . 这是数据库中数据的概述:

products table

categories table

category_product table

如何显示产品1的类别及其父类和子类别?我希望输出像

Product 1 

Category:
 Electronic
  Computer
  Phone

更新:

所以,我做的另一件事是添加$ product->类别,但代码只会列出Product1与其所有子类别一起的父类别 . 它不会过滤特定于Product1的子类别

@foreach($product->categories as $item)
        @if($item->children->count() > 0 )
            <li>
                {{ $item->name }}
                <ul>
                    @foreach($item->children as $submenu)
                        <li>{{ $submenu->name }}</li>
                    @endforeach
                </ul>
            </li>
        @endif
    @endforeach

所以,而不是像这样的输出(我想要的):

Category:
 Electronic
  Computer
  Phone

它将列出这样(这不是我想要的):

Category:
 Electronic
  Computer
  Phone
  Gadget

1 回答

  • 1

    大致如此:

    调节器

    $product = Product::with('categories.parent')->findOrFail($id);
    $categories = $product->categories->groupBy('parent_id');
    

    @foreach($categories as $parent_id => $children)
    
        @if($children->first()->parent}}
    
        Parent {{$children->first()->parent->name}}
        @foreach($children as $child)
             //print child
        @endforeach
    
        @endif
    
    @endforeach
    

相关问题