douhan8581 2014-10-07 02:27
浏览 50
已采纳

Laravel:发送变量失败(未定义变量)

I'm new at Laravel. and i got a little problem with view.

here is my controller

public function main_menu()
{   
    $mainmenus = DB::table('main_menus')->get();

    $subs = $mainmenus;
    foreach ($subs as $key => $value) 
    {
        $subs[$key] = DB::table('sub_menus')
                        ->where('parent_id', '=' , $value->main_id)
                        ->get();
    }

    $products = DB::table('sub_menus')->get();
    foreach ($products as $key => $value) {
        $products[$key] = DB::table('products')
                                -> where('product_parent', '=', $value->sub_id)
                                ->get();
    }

    $data = array("mainmenus" => $mainmenus,
            "submenus" => $subs,
            "productmenus" => $products);

    return View::make('layouts/nav', $data);

}

and here is my layouts/nav.blade.php

 @extends('layouts_index')

 @section('nav')
 <div class="nav">
 <nav class="navbar">
    <ul id="menu">

        <li class="active"><a href="index" class="drop"><i class="icon-home icon-small">  </i>Home</a></li>

        @foreach ($mainmenus as $key=>$mainmenu) 
            <li>
                <a href="menu/{{ $mainmenu->main_id }}" class="drop">
                <i class="icon-{{ $mainmenu->main_icon }} icon-small"></i>{{ $mainmenu->main_title_id }}</a>
                @if( $mainmenu->main_desc_id != "")
                    <div class="dropdown_3columns">
                        <div class="col_4">
                            <h3><i class="icon-{{ $mainmenu->main_icon }} icon-large"></i>{{ $mainmenu->main_title_id }}</h3>
                            <h4>{{ $mainmenu->main_desc_id }}</h4>  
                        </div>
                        @foreach ($submenus[$key] as $key=>$submenu) 
                            <div class="col_1">
                                <a href="" ><h5><i class="icon-small icon-bookmark"></i>{{ $submenu->sub_title_id }}</h5></a>   

                                <ul>
                                @foreach ($productmenus[$key] as $productmenu)
                                    <li>
                                        <a href="" ><i class="icon-small icon-th-large"></i>{{ $productmenu->product_title_id }}</a>
                                    </li>   
                                @endforeach

                                </ul>
                            </div>

                        @endforeach

                    </div>
                @endif
            </li>
        @endforeach
        <li class="last"><a href="#" class="drop "><i class="icon-pencil icon-small"></i>Tentang Kami</a></li>
    </ul> 
  </nav>
 </div>

@stop

my routes:

  Route::get('/', 'ShowsController@index');

when i try to run the index.blade.php it says: Undefined variable: mainmenus (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php) (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php) (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php)

what i want to do is i want to show main_menu at every view pages. i've tried changed view::make to view::share. but no luck. is there anything i miss at here?

  • 写回答

1条回答 默认 最新

  • doudi1750 2014-10-07 03:03
    关注

    Use a view composer for the menu/nav, so you don't have to build the navigation in every view manually, for example, put this code in a file named nav.php and put the file inside app/start folder then include it from app/start/global.php file:

    // app/start/nav.php
    View::composer('layouts_index', function($view){
    
        // Prepare the menuitems here
    
        $mainmenus = DB::table('main_menus')->get();
        $subs = 'Prepare it here...';
        $productmenus = 'Prepare it'
    
        return $view->with('mainmenus', $mainmenus)
                    ->with('submenus', $submenus)
                    ->with('products', $products);
    
    });
    

    In your app/start/global.php file (at the bottom) add this:

    require 'nav.php';
    

    Now, every time you extend the layouts_index.blade.php your menu items ($mainmenus, $submenus, $productmenus) will be available in the main layout. So, you may use something like this in the layout:

    @if(isset($mainmenus))
        @foreach ($mainmenus as $key => $mainmenu)
            {{ 'Use it here...' }}
        @endforeach
    @endif
    

    Now, in your Controller, when you are passing any data to any view that extends layouts_index you don't need to pass the menu items manually, because those will be passed by Framework by view composer you have created.

    If you don't want to pass the menu items for products then just remove the ->with('products', $products) from your view composer, so only $mainmenus and $submenus will be passed automatically to the main layout and you can pass the $products manually from any controller. Check this article.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作