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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效