title: List out subcategories under parentcategory based on product
body: <p>As mentioned in the title, the problem is how to list the subcategories under the parent category based on the product. I have no problem listing out the parent category and all its subcategories as I have shown in the sample code at the end of this post. But I need to list out the subcategories based on the product.</p>
  
  <p>This is the sample structure of my categories:</p>
  
  <blockquote>
  <pre><code>Electronic
  -Computer
  -Phone
  -Gadget
  
  Grocery
  -Food
  -Drinks
  </code></pre>
  </blockquote>
  
  <p><strong>This is my products table migration:</strong></p>
  
  <pre><code>   Schema::create('products', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;decimal('price')-&gt;nullable();
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>This is my categories table migration</strong></p>
  
  <pre><code>   Schema::create('categories', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('parent_id')-&gt;nullable();
          $table-&gt;string('name')-&gt;nullable();
          $table-&gt;string('description')-&gt;nullable();
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>and this one is the <strong>category_product</strong> which acts as for <strong>many to many</strong> table between <strong>category</strong> and <strong>product</strong>:</p>
  
  <pre><code>    Schema::create('category_product', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('product_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('product_id')-&gt;references('id')-&gt;on('products')-&gt;onDelete('cascade');
          $table-&gt;integer('category_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>I have setup all the relationship. Here are my models:</p>
  
  <p>This is my category model:</p>
  
  <pre><code>class Category extends Model
  {
      protected $table = 'categories';
  
      protected $fillable = [
          'name',
      ];
      public function products()
      {
          return $this-&gt;belongsToMany('App\Product');
      }
  
      public function parent()
      {
          return $this-&gt;belongsTo('App\Category', 'parent_id');
      }
  
      public function children()
      {
          return $this-&gt;hasMany('App\Category', 'parent_id');
      }
  
  }
  </code></pre>
  
  <p>This is my product model:</p>
  
  <pre><code>class Product extends Model
  {
     public function categories()
     {
       return $this-&gt;belongsToMany('App\Category');
     }
  }
  </code></pre>
  
  <p>This is my ProductController.php, I'm able to display all parents category with its subcategories using this code:</p>
  
  <pre><code>public function show($id)
   {   
        $product = Product::findOrFail($id); 
        $categories = Category::with('children')-&gt;get();
        return view('products.show', compact('product','categories'));
  
   }
  </code></pre>
  
  <p>So my product.shows.blade looks like this:</p>
  
  <pre><code>@foreach($categories as $item)
      @if($item-&gt;children-&gt;count() &gt; 0 )
          &lt;li&gt;
              {{ $item-&gt;name }}
              &lt;ul&gt;
                  @foreach($item-&gt;children as $submenu)
                      &lt;li&gt;{{ $submenu-&gt;name }}&lt;/li&gt;
                  @endforeach
              &lt;/ul&gt;
          &lt;/li&gt;
      @endif
  </code></pre>
  
  <p>@endforeach</p>
  
  <p>//OUTPUT:</p>
  
  <pre><code>Electronic  
   Computer
   Phone
   Gadget
  
  Grocery
   Food
   Drinks
  </code></pre>
  
  <p>But let say that this particular product (called Product 1) has the parent category of <strong>Electronic</strong> and the subcategory is <strong>Computer</strong> and <strong>Phone</strong> which I already attached them in the database. This is the overview of the data from the database:</p>
  
  <p><a href="https://i.stack.imgur.com/0WCba.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0WCba.jpg" alt="products table"></a></p>
  
  <p><a href="https://i.stack.imgur.com/xI0PQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xI0PQ.jpg" alt="categories table"></a></p>
  
  <p><a href="https://i.stack.imgur.com/l07Gr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l07Gr.jpg" alt="category_product table"></a></p>
  
  <p>How do I display the category of Product 1 with its parent and subcategories? I want the output to be like </p>
  
  <pre><code>Product 1 
  
  Category:
   Electronic
    Computer
    Phone
  </code></pre>
  
  <p>Update:</p>
  
  <p>So, another thing that I have done was adding the $product->categories but the code will only list out a parent category that the Product1 has with all its subcategories. It doesn't filter the subcategories specific to the Product1</p>
  
  <pre><code>   @foreach($product-&gt;categories as $item)
          @if($item-&gt;children-&gt;count() &gt; 0 )
              &lt;li&gt;
                  {{ $item-&gt;name }}
                  &lt;ul&gt;
                      @foreach($item-&gt;children as $submenu)
                          &lt;li&gt;{{ $submenu-&gt;name }}&lt;/li&gt;
                      @endforeach
                  &lt;/ul&gt;
              &lt;/li&gt;
          @endif
      @endforeach
  </code></pre>
  
  <p>So, instead of the output like this (which i want):</p>
  
  <pre><code>Category:
   Electronic
    Computer
    Phone
  </code></pre>
  
  <p>it will list out like this (which is not i want):</p>
  
  <pre><code>Category:
   Electronic
    Computer
    Phone
    Gadget
  </code></pre>