1 year ago

How to decode json array

Posted 1 year ago by ziben69

Hello guys,

I need to display titles from database in sidebar, and I did something like this: AppServiceProvider.php

   view()->composer('*', function($view){

            $results = [];
            $results['zone'] = Zone::all()->last();
            $results['tourism'] = Tourism::all()->last();
            $results['business'] = Business::all()->last();
            $results['culture'] = Culture::all()->last();
            $results['education'] = Education::all()->last();
            $results['inhabitant'] = Inhabitant::all()->last();
            $results['investor'] = Investor::all()->last();
            $results['senior'] = Senior::all()->last();
            $results['sport'] = Sport::all()->last();

            $view->with('sidebar', $results); 

it's almost okay, I get a result in the form of a json array that looks like this:

{"id":1,"title":"test","description":"<p>test<\/p>","path":"dsadsa","created_at":"2019-01-03 10:25:33","updated_at":"2019-01-03 10:25:33"} {"id":2,"title":"rtest2","description":"<p>dssads<\/p>","path":"dsadas","created_at":"2019-01-03 10:33:58","updated_at":"2019-01-03 10:33:58"}...

because I have a json result, I can not only display the title. Below fragment of the view:

    @foreach($sidebar as $type => $list)
           {{ $list }}

I need do something like this:

    @foreach($sidebar as $type => $list)
    @foreach($list as $item)
           {{ $item->title }}

but I have exception:

Invalid argument supplied for foreach().

Thanks for help.

Please sign in or create an account to participate in this conversation.