Search code examples
laravelforeacheloquentrelationshipnested-loops

Getting trouble in nested loop when returning children of parents in same table Laravel 5.6


I have a table account_heads 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 loop is something like bellow-

@foreach ($accountHeads as $accountHead)
     @foreach ($accountHead->children as $children)
         <option value="{{ $children->id }}">{{ $children->name }} @if($children->code) ({{ $children->code }})@endif</option>
         @foreach($children->children as $c1)
            <option value="{{ $c1->id }}"> -- {{ $c1->name }} @if($c1->code) ({{ $c1->code }})@endif</option>
            @foreach($c1->children as $c2)
                <option value="{{ $c2->id }}"> ---- {{ $c2->name }} @if($c2->code) ({{ $c2->code }})@endif</option>
                @foreach($c2->children as $c3)
                      <option value="{{ $c3->id }}"> ------ {{ $c3->name }} @if($c3->code) ({{ $c3->code }})@endif</option>
                      @foreach($c3->children as $c4)
                      <option value="{{ $c4->id }}"> -------- {{ $c4->name }} @if($c4->code) ({{ $c4->code }})@endif</option>
                         @foreach($c4->children as $c5)
                         <option value="{{ $c5->id }}"> ---------- {{ $c5->name }} @if($c5->code) ({{ $c5->code }})@endif</option>
                            @foreach($c5->children as $c6)
                            <option value="{{ $c6->id }}"> ------------ {{ $c6->name }} @if($c6->code) ({{ $c6->code }})@endif</option>
                             @endforeach
                      @endforeach
                 @endforeach
              @endforeach
          @endforeach
       @endforeach
    @endforeach
@endforeach

I want to minimize the above loop. I want to get the result is like bellow- enter image description here

My database table structure is bellow- enter image description here

And my account AccountHeadsController's create method is bellow-

public function create()
{

    $accountHeads = AccountHead::with('children')->where('parent_id', 0)->get();
    return view('account.account-head.create', compact('accountHeads'));
}

And AccountHead model is like bellow-

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->belongsTo('App\Model\AccountHead','parent_id')->where('parent_id',0)->with('parent');
    }

    public function children()
    {
    return $this->hasMany('App\Model\AccountHead','parent_id')->with('children');
    }
}

Solution

  • I have done this myself like this:

    Created Helper File: App/Helpers/MyHelper.php

    MyHelper.php file:

    <?php
    namespace App\Helpers;
    
    class MyHelper
    {
      public static function dd($array, $step = 0)
    {
        $output = '';
            foreach($array as $arr)
            {
                $output .= '<option  value="'.$arr->id.'">'.str_repeat('&nbsp;&nbsp;&nbsp;',$step). $arr->name.'</option>';
                if($arr->children)
                {
                    $output .= self::dd($arr->children, $step+1);
                }
            }
        return $output;
    }
    }
    

    and add it to /config/app.php under aliases.

    'MyHelper'  => App\Helpers\MyHelper::class,
    

    then in your view you can use:

    <select>
     {!! MyHelper::dd($accountHeads) !!}
    </select>