【问题标题】:Laravel: Error in Passing a multidimensional array from controller to view in LaravelLaravel:将多维数组从控制器传递到 Laravel 中的视图时出错
【发布时间】:2014-04-23 06:58:22
【问题描述】:

路由文件:-

Route::get('/',function()
{
 $menuitem = Menuitem::all(); 
 return VIEW::make('index',$menuitem); 
});

index.blade.php:-

@foreach($menuitem as $item)
 {{$item['item_link']}} 
@endforeach

我在index.blade.php 中收到错误Undefined variable $menuitem

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    试试

    Route::get('/',function()
    {
        $menuitem = Menuitem::all(); 
        return View::make('index')->with('menuItem', $menuItem); 
    }
    

    【讨论】:

    • 终于可以访问 view 中的数组了。感谢您的支持
    【解决方案2】:

    View::make 接受一个数组作为第二个参数。

    public View make(string $view, array $data = array(), array $mergeData = array())
    

    应该是这样的:

    Route::get('/',function()
    {
      $menuitem = Menuitem::all();
    
      $data = array(
                'menuitemKey' => $menuitem
              );
    
      return View::make('index', $data); 
    }
    

    然后你可以像这样访问它:

    @foreach($menuitemKey as $item)
      {{$item['item_link']}} 
    @endforeach
    

    【讨论】:

      【解决方案3】:

      这对我有用。

      'Route('/',function(){
      $myarray = Menuitem::all();
      return View::make('myview')->with('data',$myarray);
      })'
      

      myview.blade.php

      '@for($i=0;$i<sizeof($data),$i++)
       {{$data[$i]['item']}}
        @endfor'
      

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 2017-05-25
        • 1970-01-01
        • 2016-01-21
        • 2018-01-05
        • 2017-01-29
        • 2017-04-05
        • 2015-07-25
        相关资源
        最近更新 更多