Problem when displaying data

1

I have this script at the moment of executing it shows me error

Tables:

Controller

$menu=DB::table('menu')
                    ->select('menu','url','id_menu')->get();

    $submenu=DB::table('sub_menu')->join('menu','sub_menu.id_menu','=','menu.id_menu')
                    ->select('sub_menu.nombre','sub_menu.url')->get();

    return view('home', compact('menu','submenu', 'broker'));

Vista

<ul>
    @foreach($menu as $item)
        <li>
            <a href="{{ $item['url'] }}">{{ $item['menu'] }}</a>
            @if (count($item['submenu'])) 
                <ul>
                @foreach ($item['submenu'] as $subitem)
                    <li><a href="{{ $subitem['url'] }}">{{ $subitem['nombre'] }}</a></li>
                @endforeach
                </ul>
            @endif
        </li>
    @endforeach
  </ul>

Error

    
asked by Oswuell 09.08.2017 в 23:14
source

1 answer

1

The problem is that you are using a StdClass as if it were a array , here you can see (Laravel's documentation) as they say that what they will return is a StdClass

link

How to get a array ? It's easy, after ->get() you should put a ->toArray() that way you get the results and pass it to array .

    
answered by 10.08.2017 в 09:16