title: get subcategory in laravel or livewire
body: <p>I'm using Livewire version 2. I wanted to show all the categories on the site.
  Mother category = 0
  The subset of these categories is <code>parent_id = 1</code>. How can I get this subset
  relation?</p>
  <p>For example :
  I have a category called TV so far ok
  I want to add a Samsung or LG category to this TV.</p>
  <p><strong>ProductCategory model</strong>:</p>
  <pre class="lang-php prettyprint-override"><code>    public function parent_name($id)
      {
          $name_parent = ProductCategory::where('id' , $id)-&gt;first();
  
          if (!$name_parent) {
              return 'دسته بندی مادر';
          } else {
              $name_parent = $name_parent-&gt;topic;
              return $name_parent;
          }
      }
  </code></pre>
  <p><strong>Livewire component</strong>:</p>
  <pre><code>class HeaderBottom extends Component
  {
      public $category;
  
      public function mount(ProductCategory $category)
      {
          $this-&gt;category = $category;
      }
  
      public function render()
      {
          $a = 1;
          $allCategories = ProductCategory::where('parent_id', '=', 0)-&gt;latest()-&gt;get();
          $subcategory = ProductCategory::where('parent_id', '=', $a)-&gt;latest()-&gt;get();
  
          return view('livewire.site.layout.header.header-bottom', compact(['allCategories','subcategory']));
      }
  }
  </code></pre>
  <p><strong>my blade</strong>:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;div class=&quot;dropdown-menu&quot;&gt;
    &lt;nav class=&quot;side-nav&quot;&gt;
      &lt;ul class=&quot;menu-vertical sf-arrows&quot;&gt;
        {{-- 
        &lt;li class=&quot;item-lead&quot;&gt;
          &lt;a href=&quot;{{ route('site.productCategory.index', 2) }}&quot;&gt;
            تخفیف
                                          های روزانه
          &lt;/a&gt;
        &lt;/li&gt;
        &lt;li class=&quot;item-lead&quot;&gt;
          &lt;a href=&quot;{{ route('site.productCategory.index', 3) }}&quot;&gt;
            هدیه
                                          ها
          &lt;/a&gt;
        &lt;/li&gt;
        --}}
  
        @foreach ($allCategories as $category)
          &lt;li&gt;
            &lt;a href=&quot;{{ route('site.productCategory.index', $category-&gt;id) }}&quot;&gt;
              {{ $category-&gt;topic }}
            &lt;/a&gt;
          &lt;/li&gt;
        @endforeach
        
        &lt;li class=&quot;megamenu-container&quot;&gt;
          &lt;a class=&quot;sf-with-ul&quot; href=&quot;#&quot;&gt;
            {{ $category-&gt;parent_name($category-&gt;parent_id) }}
          &lt;/a&gt;
          &lt;div class=&quot;megamenu&quot; style=&quot;display: none;&quot;&gt;
            &lt;div class=&quot;row no-gutters&quot;&gt;
              &lt;div class=&quot;col-md-8&quot;&gt;
                &lt;div class=&quot;menu-col&quot;&gt;
                  &lt;div class=&quot;row&quot;&gt;
                    &lt;div class=&quot;col-md-6&quot;&gt;
                      @foreach ($subcategory as $item)
                        &lt;div class=&quot;menu-title&quot;&gt;
                          &lt;a href=&quot;{{ route('site.productCategory.index', $item-&gt;id) }}&quot;&gt;
                            {{ $item-&gt;topic }}
                          &lt;/a&gt;
                        &lt;/div&gt;
                      @endforeach
                    &lt;/div&gt;&lt;!-- End .col-md-6 --&gt;
                  &lt;/div&gt;&lt;!-- End .row --&gt;
                &lt;/div&gt;&lt;!-- End .menu-col --&gt;
              &lt;/div&gt;&lt;!-- End .col-md-8 --&gt;
              &lt;div class=&quot;col-md-4&quot;&gt;
                &lt;div class=&quot;banner banner-overlay&quot;&gt;
                  &lt;a href=&quot;category.html&quot; class=&quot;banner banner-menu&quot;&gt;
                    &lt;img src=&quot;assets/images/demos/demo-13/menu/banner-2.jpg&quot; alt=&quot;بنر&quot;&gt;
                  &lt;/a&gt;
                &lt;/div&gt;&lt;!-- End .banner banner-overlay --&gt;
              &lt;/div&gt;&lt;!-- End .col-md-4 --&gt;
            &lt;/div&gt;&lt;!-- End .row --&gt;
          &lt;/div&gt;&lt;!-- End .megamenu --&gt;
        &lt;/li&gt;
      &lt;/ul&gt;&lt;!-- End .menu-vertical --&gt;
    &lt;/nav&gt;&lt;!-- End .side-nav --&gt;
  &lt;/div&gt;
  </code></pre>