dql7588 2016-12-01 02:03
浏览 43
已采纳

如何将多个数组传递给作曲家laravel

I'm trying to pass 2 arrays to a base view in laravel but I can't find the way to figure it out. It works well with 1 array, but can't with 2 arrays. I have a sidebar for a commercer website, which will display list of catetogories and list of shops. I use composer to handle it as a base view, but I can't pass 2 arrays of categories and shops. This is my composer class:

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);
    }
}

Here is my view (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

I have no experiences with laravel. Please help me, thanks very much.

  • 写回答

1条回答 默认 最新

  • dongzang7182 2016-12-01 02:23
    关注

    you can use an array to trans many value to blade.

    hope it helps.

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

    when you have to many values, use this

    $data['categories'] = $categories;
    $data['shops'] = $shops;
    $data[...] = ...
    ...
    $view->with($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?