Create Dynamic Navigation in Laravel

In this blog post, we learn how to make the dynamic navigation bar in Laravel 5. we fetch data from the database and show into the navigation bar.

Many projects need dynamic nav bar where the user needs dynamic data in the navigation bar when any new data, should be shown in the navigation bar. so in this blog post, we learn how to create dynamic navigation in Laravel

Step:1 Create a  new Laravel Application.

laravel new navigation

After successful install Laravel Application, we move to the next step.

Step:2 Create some data table and insert database.

We required some data to show on navigation bar so insert dummy data.

Step:3 Add some code in AppServiceProvider.php 

Create Dynamic Navigation in Laravel

Here we call some data using model and pass into all view so we can print that data in the whole project view.

<?php

namespace App\Providers;
use App\Navbar;
use View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
   
    public function register()
    {
        //
    }

    
    public function boot()
    {
        Schema::defaultStringLength(191);
        View::composer('*', function($view)
        {
            $navitem= Navbar::all();
            $view->with('navitem', $navitem);


        });
    }
}

Here we use the navbar table to show all user record in the navigation bar.

Step:4 Show in View File

Now use this flowing code at your nav bar or where you want to show dynamic navigation menu.

   @foreach ($navitemas $item)
         <p>{{$item->name}} </p>
   @endforeach

So here we completed the tutorial on how to create dynamic navigation in Laravel.

Thank you…