0

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.

2
  • 1
    I don't have a laravel install to try this out, have you tried something like ->with(array('categories'=>$categories, 'shops'=>$shops));? Commented Dec 1, 2016 at 2:15
  • Thank you very much, it works perfectly, but I've just realized that my way works. I just miss ";" at the end of array, I've wasted 2 hours for nothing. Anyway, thanks for your help. Commented Dec 1, 2016 at 2:33

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it works perfectly, but I've just realized that my way works. I just miss ";" at the end of array, I've wasted 2 hours for nothing. Anyway, thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.