<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: musthafa-advance</title>
    <description>The latest articles on DEV Community by musthafa-advance (@musthafaadvance).</description>
    <link>https://dev.to/musthafaadvance</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F429258%2F2603486a-c68b-4547-b5b0-939a98e03dd5.jpeg</url>
      <title>DEV Community: musthafa-advance</title>
      <link>https://dev.to/musthafaadvance</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/musthafaadvance"/>
    <language>en</language>
    <item>
      <title>How to create a nested-list of categories in Laravel blade</title>
      <dc:creator>musthafa-advance</dc:creator>
      <pubDate>Sun, 12 Jul 2020 12:30:40 +0000</pubDate>
      <link>https://dev.to/musthafaadvance/how-to-create-a-nested-list-of-categories-in-laravel-blade-3i4l</link>
      <guid>https://dev.to/musthafaadvance/how-to-create-a-nested-list-of-categories-in-laravel-blade-3i4l</guid>
      <description>&lt;p&gt;Its really difficult to iterate and show Parent child Categories tree or listing in Laravel, sometimes it affect websites performance, &lt;br&gt;
To solve this problem we need to use a recursive blade.&lt;br&gt;
Just follow this steps to make it happen&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First of all we need to get all the categories and childs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To get all categories with childs, create a childs&lt;br&gt;
function inside the Category model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public function childs()
    {
        return $this-&amp;gt;hasMany(Category::class, 'parent_id', 'id');
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;this function will return all categories with its childs &lt;br&gt;
then fetch categories in your controller like below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$categories = Category::whereNull('parent_id')-&amp;gt;with('childs')-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2.inside your blade&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@foreach($categories as $category)
  &amp;lt;li&amp;gt;
   {{{ $category-&amp;gt;category_name }}}
   @if(count($category-&amp;gt;childs))
   @include('category-partial ',['childs' =&amp;gt; $category-&amp;gt;childs])
   @endif
  &amp;lt;/li&amp;gt;
@endforeach
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3.Create category-partial.blade which we are using to loop each of the category so that we can show the category without a limit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
@foreach($childs as $child)
    &amp;lt;li&amp;gt;
 &amp;lt;a href="{{{  url('search/?category='.$child-&amp;gt;category_name.'-'.$child-&amp;gt;id.'&amp;amp;q=') }}}"&amp;gt;{{{ $child-&amp;gt;category_name }}}&amp;lt;/a&amp;gt;
 @if(count($child-&amp;gt;childs))
 @include('admin.managechild',['childs' =&amp;gt; $child-&amp;gt;childs])
 @endif
  &amp;lt;/li&amp;gt;
@endforeach
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>php</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
