简体   繁体   中英

How to Create Dynamic Menu bar in Laravel using Database

initially, I created the static navigation bar but now I want to improve navigation bar as Dynamically.

in there im try function inside the Controller but it was not success because that view manage only one route.i need to that view in every pages of my web.

SideMenuController.php:

<?php 
   namespace App\Http\Controllers; 
   
   use Illuminate\Http\Request; 
   use App\admins; 
   use DB; 

  class SideMenuController extends Controller { 
    public function index(){ 
      $details = DB::SELECT("SELECT Name FROM admins"); 
      return view('layout', compact('details')); 
    } 
  }

web.php:

 Route::get('/user/test','SideMenuController@index');

test.blade.php :

  • Order @foreach($details as $value)
  • {{ $value->Name }}
  • @endforeach

    Error Exception

    (3/3) ErrorException Undefined variable: details (View: C:\\xampp\\htdocs\\ERP\\ERP_LAR\\resources\\views\\test.blade.php) (View: C:\\xampp\\htdocs\\ERP\\ERP_LAR\\resources\\views\\test.blade.php)

    For first take a look at the blade engine that is in Laravel.

    The idea is that you will create a value in an controller and past this to the view. Here you will extract the value and place the right value to the right place. With blade you can even make a separate navigation.php and yield them in every view you need it.

    There are many ways to solve the problem.

    1. You can make a separate php file in your project like "navbar.php" and include it in by <?php include('navbar.php'); ?> <?php include('navbar.php'); ?>
    2. you can also use classes but it is harder.

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM