【问题标题】:List out subcategories under parentcategory based on product根据产品列出父类别下的子类别
【发布时间】:2016-05-08 07:41:08
【问题描述】:

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

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

Electronic
-Computer
-Phone
-Gadget

Grocery
-Food
-Drinks

这是我的产品表迁移:

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

这是我的类别表迁移

   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 之间的 ma​​ny 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();
    });

我已经建立了所有的关系。这是我的模型:

这是我的类别模型:

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)的父类别为 电子,子类别为 计算机电话我已经将它们附加到数据库中。这是数据库中数据的概述:

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

Product 1 

Category:
 Electronic
  Computer
  Phone

更新:

所以,我所做的另一件事是添加 $product->categories,但代码只会列出 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

【问题讨论】:

    标签: laravel eloquent laravel-5.1 query-builder laravel-5.2


    【解决方案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
    

    【讨论】:

    • 顺便说一句,您的代码不起作用(与我的代码相同)。此代码仅列出了父类别具有的所有子类别,例如,这将列出 product1 的父类别,即电子及其所有子类别,未进行过滤。我修复了另一部分,但我仍在寻找解决方案。不小心把这个答案投了赞成票
    • 我想要的是根据产品显示子类别。不一一列举。任何想法 ? :\
    • 哦!你想要相反的结果。
    • 我在尝试获取非对象的属性时出错。对于这个更新的答案,为什么我们需要 $product = Product::with('categories.parent')->findOrFail($id);
    • 某人也可以删除父级并留下子级。这会破坏关系,但我会落后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2018-06-10
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多