Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stefancoding's avatar

How to use loop in blade file with multy relationships?

I would like to loop in multi-sub categories that could have hundreds of subcategories in the blade file. I use only one table in database for the main and all subcategories. I use ID, PARENT_ID, and NAME columns in the database.

I have already done in model and controller to get relationships with all subcategories. My code looks like this:

Category.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id')->with('children');
    }
}

CategoryController.php

public function index()
    {
     
        
        $categories = Category::where('parent_id', '=', '0')->with(['children'])->get();
       
        
        return view('index', ['categories' => $categories]);
    }

category.balde.php

@foreach ($categories as $category)
                                    <li><a class="active" href="index.html">{{ $category->name }} <i class="fa fa-chevron-down"></i></a>
                                        <ul class="sub-menu">
                                                @foreach($category->children as $childSub)
                                                <li><a href="#">{{ $childSub->name }} {!! count($childSub->children) > 0 ? html_entity_decode('<i class="fa fa-chevron-right"></i>') : '' !!} </a>

                                                    @if(count($childSub->children) > 0 )
                                                    <ul class="level-menu level-menu-modify">
                                                        @foreach($childSub->children as $subSub)
                                                            <li><a href="blog-post-left.html">{{ $subSub->name }}</a>
                                                                
                                                            </li>
                                                        @endforeach
                                                    </ul>
                                                    @endif
                                                </li>
                                                    <li><a href="index.html"></a></li><li><a href="#"></a>
                                                      
                                                        
                                                    </li>
                                               @endforeach
                                            
                                        </ul>
                                           
                                        
                                    </li>
                                @endforeach

As you can see I am looping in blade file sub and sub sub categories but this loop that I using is not for hundreds of subcategories just only for 2 subcategories. How to loop to get automatically all sub sub sub subcategories without using hundreds of loops in the blade file?

0 likes
1 reply

Please or to participate in this conversation.