title: Getting trouble in nested loop when returning children of parents in same table Laravel 5.6
body: <p>I have a table <code>account_heads</code> in where I am returning children of parents at the same table. Till now everything was fine, but getting a problem when I am showing these in forms. I have 4 or 5 levels children of parents at the same table.
  My Present <code>loop</code> is something like bellow- </p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>@foreach ($accountHeads as $accountHead)
       @foreach ($accountHead-&gt;children as $children)
           &lt;option value="{{ $children-&gt;id }}"&gt;{{ $children-&gt;name }} @if($children-&gt;code) ({{ $children-&gt;code }})@endif&lt;/option&gt;
           @foreach($children-&gt;children as $c1)
              &lt;option value="{{ $c1-&gt;id }}"&gt; -- {{ $c1-&gt;name }} @if($c1-&gt;code) ({{ $c1-&gt;code }})@endif&lt;/option&gt;
              @foreach($c1-&gt;children as $c2)
                  &lt;option value="{{ $c2-&gt;id }}"&gt; ---- {{ $c2-&gt;name }} @if($c2-&gt;code) ({{ $c2-&gt;code }})@endif&lt;/option&gt;
                  @foreach($c2-&gt;children as $c3)
                        &lt;option value="{{ $c3-&gt;id }}"&gt; ------ {{ $c3-&gt;name }} @if($c3-&gt;code) ({{ $c3-&gt;code }})@endif&lt;/option&gt;
                        @foreach($c3-&gt;children as $c4)
                        &lt;option value="{{ $c4-&gt;id }}"&gt; -------- {{ $c4-&gt;name }} @if($c4-&gt;code) ({{ $c4-&gt;code }})@endif&lt;/option&gt;
                           @foreach($c4-&gt;children as $c5)
                           &lt;option value="{{ $c5-&gt;id }}"&gt; ---------- {{ $c5-&gt;name }} @if($c5-&gt;code) ({{ $c5-&gt;code }})@endif&lt;/option&gt;
                              @foreach($c5-&gt;children as $c6)
                              &lt;option value="{{ $c6-&gt;id }}"&gt; ------------ {{ $c6-&gt;name }} @if($c6-&gt;code) ({{ $c6-&gt;code }})@endif&lt;/option&gt;
                               @endforeach
                        @endforeach
                   @endforeach
                @endforeach
            @endforeach
         @endforeach
      @endforeach
  @endforeach
  </code></pre>
  
  <p><strong>I want to minimize the above loop.</strong>
  I want to get the result is like bellow-
  <a href="https://i.stack.imgur.com/qulP2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qulP2.png" alt="enter image description here"></a></p>
  
  <p>My database table structure is bellow-
  <a href="https://i.stack.imgur.com/ByjwI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ByjwI.png" alt="enter image description here"></a></p>
  
  <p>And my account <code>AccountHeadsController</code>'s <code>create</code> method is bellow-</p>
  
  <pre class="lang-php prettyprint-override"><code>public function create()
  {
  
      $accountHeads = AccountHead::with('children')-&gt;where('parent_id', 0)-&gt;get();
      return view('account.account-head.create', compact('accountHeads'));
  }
  </code></pre>
  
  <p>And <code>AccountHead</code> model is like bellow-</p>
  
  <pre class="lang-php prettyprint-override"><code>class AccountHead extends Model
  {
      use SoftDeletes;
      use Updater;
  
      protected $table = 'account_heads';
      protected $fillable = [
      'name', 'code', 'memo', 'opening_balance', 'parent_id', 'status', 'created_by', 'updated_by', 'deleted_by'
      ];
  
      public function parent()
      {
          return $this-&gt;belongsTo('App\Model\AccountHead','parent_id')-&gt;where('parent_id',0)-&gt;with('parent');
      }
  
      public function children()
      {
      return $this-&gt;hasMany('App\Model\AccountHead','parent_id')-&gt;with('children');
      }
  }
  </code></pre>