2016-12-01 57 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小時的空閒時間。無論如何,感謝您的幫助。 –