2016-12-01 60 views
0

我试图将2个数组传递给laravel中的基本视图,但我无法找到找出它的方法。它适用于1阵列,但不能与2阵列。 我有一个commercer网站的侧边栏,它将显示食物清单和商店列表。我使用作曲家作为基础视图来处理它,但是我无法传递2个类别和商店的数组。 这是我的作曲类:如何将多个数组传递给作曲者laravel

class SidebarComposer 
{ 

    public function compose(View $view) 
    { 
     $categories = array(); 
     $categories[0] = "Cate 1"; 
     $categories[1] = "Cate 2"; 
     $categories[2] = "Cate 3"; 
     $categories[3] = "Cate 4"; 
     $categories[4] = "Cate 5"; 
     $categories[5] = "Cate 6"; 

     $shops = array(); 
     $shops[0] = "Shop 0"; 
     $shops[1] = "shops 1" 

     $view->with('categories',$categories)->with('shops',$shops); 
    } 
} 

这是我的看法(sidebar.blade.php)

@for ($i = 0; $i < count($categories); $i++) 
    <div class="panel panel-default"> 
    <div class="panel-heading"> 
     <h4 class="panel-title"><a href="">{{$categories[$i]}}</a></h4> 
    </div> 
</div> 
@endfor 
@for ($i = 0; $i < count($shops); $i++) 
      <li><a href="{{url('')}}"> <span class="pull-right"></span>{{$shops[$i]}}</a></li> 
@endfor 

我有laravel没有经验。请帮助我,非常感谢。

+1

我没有laravel安装尝试了这一点,你有没有试过像' - >使用(阵列(“类别” => $类别,'商店'=> $商店));'? –

+0

非常感谢你,它完美的作品,但我刚刚意识到我的方式有效。我只是想念“;”在阵列的末尾,我浪费了2小时的空闲时间。无论如何,感谢您的帮助。 –

回答

1

您可以使用阵列将许多值转换为刀片。

希望它有帮助。

$view->with(['categories' => $categories, 'shops' => $shops]); 

当你有很多的价值观,用这个

$data['categories'] = $categories; 
$data['shops'] = $shops; 
$data[...] = ... 
... 
$view->with($data); 
+0

非常感谢你,它完美的工作,但我刚刚意识到我的方式有效。我只是想念“;”在阵列的末尾,我浪费了2小时的空闲时间。无论如何,感谢您的帮助。 –