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

david001's avatar

How to make menu and submenu dynamic

I want to make menu and submenu dynamic .I made menu with their link but unable to make submenu,please help me my database structure table name:menu

id              menu_name       url                timestamps

table:sub_menu

id      submenu_name       link      menu_id  timestamps

my query is like this

public function menu()
{


   $sql=\DB::table('menu')->select('menu_name','url','id')->get();

   $submenu=\DB::table('sub_menu')->join('menu','sub_menu.menu_id','=','menu.id')
   ->select('submenu_name','link')->get();
          
    return view('products.show.menu',compact('sql','submenu'));
}

view

@foreach($sql as $s)
<ul>
    <li><a href="{{$s->url}}">{{$s->menu_name}}</a></li>

</ul>
@endforeach

how to make submenu dropdown under parent menu

0 likes
13 replies
Corez64's avatar
Corez64
Best Answer
Level 37

I think this would be easier if you were using Eloquent models for your menu items as you could load the submenu items.

// Menu Model
class Menu extends Model
{
    public function submenu()
    {
        return $this->hasMany(SubMenu::class);
    }
}

// SubMenu Model
class SubMenu extends Model
{
    public function menu()
    {
        return $this->belongsTo(Menu::class);
    }
}

// Where ever you want your menu
public function menu()
{
    $menu = Menu::all()->load('submenu');

    return view('products.show.menu',compact('menu'));
}

Then your view will be something like this:

<ul>
@foreach($menu as $item)
    <li>
        <a href="{{$item->url}}">{{$item->menu_name}}</a>
        @if ($item->submenu->count()) 
            <ul>
            @foreach ($item->submenu as $subitem)
                <li><a href="{{$subitem->url}}">{{$subitem->menu_name}}</a></li>
            @endforeach
            </ul>
        @endif
    </li>
@endforeach
</ul>
2 likes
david001's avatar

@Corez64 i like the way you mention above but i got error on this line @foreach ($item->submenu as $subitem) error is Invalid argument supplied for foreach() (View:C:\xampp\htdocs\...... what this mean?

Corez64's avatar

It means that the $item->submenu is being populated by an empty collection which will evaluate to true in the @if. If you change the @if ($item->submenu) to @if ($item->submenu->count()) and see if it works then. I have updated my previous post to reflect this change.

david001's avatar

@Corez64 it is still not working FatalErrorException in............in line 5 in @if ( $item->submenu->count()) line

Call to a member function count() on string

Corez64's avatar

Submenu shouldn't be a string... At the top of your view products.show.menu could you add the code {!! dd($menu->toArray()) !!}, expand all the trees and paste the result here so I can see your data structure please?

david001's avatar

i got nothing Call to a member function toArray() on array

Corez64's avatar

Did you create the models as I suggested?

david001's avatar

lateron i will do that for now i just want to check result by this way,after it works i will do the same model you suggested to me please.. public function menu() {

   $menu=\DB::table('menu')->rightjoin('sub_menu','menu.id','=','sub_menu.menu_id')
   ->select('submenu_name','link','url','menu_id','menu_name','menu.id')->get();
      
    return view('products.show.menu',compact('menu'));
}
david001's avatar

@Corez64 could you help me now

This is the tree

array:3 [▼
  0 => array:6 [▼
    "id" => 1
    "menu_name" => "Home"
    "url" => "localhost/phpmyadmi"
    "created_at" => "-0001-11-30 00:00:00"
    "updated_at" => "-0001-11-30 00:00:00"
    "submenu" => []
  ]
  1 => array:6 [▼
    "id" => 2
    "menu_name" => "Contact "
    "url" => "http://localhost:8000/admin/menu"
    "created_at" => "-0001-11-30 00:00:00"
    "updated_at" => "-0001-11-30 00:00:00"
    "submenu" => array:2 [▼
      0 => array:6 [▼
        "id" => 1
        "submenu_name" => "contact A"
        "link" => "http://localhost/phpmyadmin/"
        "menu_id" => 2
        "created_at" => "-0001-11-30 00:00:00"
        "updated_at" => "-0001-11-30 00:00:00"
      ]
      1 => array:6 [▼
        "id" => 2
        "submenu_name" => "ContacB"
        "link" => "http://localhost/phpmyadmin/123"
        "menu_id" => 2
        "created_at" => "-0001-11-30 00:00:00"
        "updated_at" => "-0001-11-30 00:00:00"
      ]
    ]
  ]
  2 => array:6 [▼
    "id" => 3
    "menu_name" => "About"
    "url" => "https://www.youtube.com/watch?v=yq_KpZTrEzE"
    "created_at" => "-0001-11-30 00:00:00"
    "updated_at" => "-0001-11-30 00:00:00"
    "submenu" => []
  ]
]
Corez64's avatar

Since you aren't using models you will need remove the ->count() from the if submenu check. You will probably need to change object syntax to array syntax inside the second foreach loop and it should work then.

willvincent's avatar

This should work with that structure.

<ul>
@foreach($menu as $item)
    <li>
        <a href="{{ $item['url'] }}">{{ $item['menu_name'] }}</a>
        @if (count($item['submenu'])) 
            <ul>
            @foreach ($item['submenu'] as $subitem)
                <li><a href="{{ $subitem['url'] }}">{{ $subitem['menu_name'] }}</a></li>
            @endforeach
            </ul>
        @endif
    </li>
@endforeach
</ul>
david001's avatar

Thanks now it working but why there is no need to use query builder

Neeraj1005's avatar

@david001 How can I put the url or link in the database? And after put the url in database how to use this or how to code for this in blade file.

Please or to participate in this conversation.