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

shahr's avatar
Level 10

How to import categories in menu and submenu?

I want to create a megamenu And I want to import categories to the menu and sub-menu for link.

I have a categories table.

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name_fa');
        $table->string('name_en');
        $table->integer('parent_id');
        $table->string('icon')->nullable();
        $table->timestamps();
    });
}

AppSeriveceProvider.php

public function register()
{
    Schema::defaultStringLength(191);
    view()->composer('*', function($view) {
        $view->with('menus', Category::whereParent_id('0')->get());
    });
}

header.blade.php

 <ul class="navbar-nav mr-auto">
 @foreach($menus as $menu)
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
            <i class="fas {{ $menu->icon }} fa-2x"></i>
            <span>{{ $menu->name_fa }}</span>
        </a>
        <div class="dropdown-menu w-100">
            <div class="px-0 container">
                <div class="row">
                    @foreach($menu->getChild as $submenu)
                        <div class="col-md-4">
                            <a class="dropdown-item text-right" href="#">
                                {{ $submenu->name_fa }}
                            </a>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </li>
    @endforeach
    </ul>

Category.php

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

I see blanks sub menu in my menu.

0 likes
4 replies
chr15k's avatar

Couple things:

The relation getChild is a bit confusing since a child is a singular noun. I would personally use the plural noun children which makes sense with a hasMany relation. This also aligns with Laravel's naming conventions.

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

Secondly, you could eager load the relationship with the menu collection so that you're reducing the number of database queries:

view()->composer('*', function($view) {
    $view
       ->with('menus', Category::whereParent_id('0')
       ->with('children')
       ->get());
});

Thirdly, what do your categories look like? Looking at your code, I don't see how you would see blank submenus, unless the categories stored don't have relating ids?

shahr's avatar
Level 10

@chr15k

I changed your codes but my problem did not solve yet.

shahr's avatar
Level 10

I changed my codes...!

     <ul class="navbar-nav ml-auto">
        @foreach($menus as $menu)
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="fas {{ $menu->icon }} fa-2x"></i>
                    <span>{{ $menu->name_fa }}</span>
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    @foreach($menu->getChild as $submenu)
                        <a class="dropdown-item" href="#">{{ $submenu->name_fa }}</a>
                    @endforeach
                </div>
            </li>
        @endforeach
    </ul>

this code did not show last a link . and I want to create megamenu in bootstrap

Please or to participate in this conversation.