简体   繁体   English

如何使用数据库在 Laravel 中创建动态菜单栏

[英]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: 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:网页.php:

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

test.blade.php : test.blade.php :

  • Order @foreach($details as $value)订购@foreach($details as $value)
  • {{ $value->Name }} {{ $value->姓名}}
  • @endforeach @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) (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\\视图\\test.blade.php)

    For first take a look at the blade engine that is in Laravel.首先看一下 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.使用刀片,您甚至可以制作一个单独的 navigation.php 并在您需要的每个视图中生成它们。

    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 文件,例如“navbar.php”,并通过<?php include('navbar.php'); ?>将其包含在其中<?php include('navbar.php'); ?> <?php include('navbar.php'); ?>
    2. you can also use classes but it is harder.你也可以使用类,但它更难。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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