samedi 27 mai 2017

Json decode when I iterate with forearch in laravel 5.2

So I have a table called Categories and this is his Seeder file:

SubCategories::create(array(
        'id' => '1',
        'category' => 'Books',
        'image' => '2.png',
        'subcategory' => '{"0":"Action","1":"Drama","3":"Romance","4":"Biography", "5":"Mystery"}'
    ));

What I want to do is to echo this in a view file:

@foreach($SubCategories as $SubCategory)
   <li>
     <figure>
       <div><img src="pictures/images/"></div>
         <figcaption>
             <h3></h3>
             <br>
             <div class="cat_men">
                 <a href="#"></a>
             </div>
         </figcaption>
    </figure>
  </li>
@endforeach

This unfortunately echoes back at my site something like this:

{"0":"Action","1":"Drama","3":"Romance","4":"Biography","5":"Mystery"}

I so when I try to use json_decode function with the following code:

@foreach(json_decode($SubCategory->subcategory,true) as $sub)
   <li>  </li>
@endforeach

I get the following error:

ErrorException in de1a4fc568193a3fb9e08163f0b034f349f40fd8.php line 51: Trying to get property of non-object

This is my controller:

public function getIndex() { 
   $SubCategories = SubCategories::all();
   return view('shop.index')->with('SubCategories',$SubCategories);
}

Anyone has any idea for this problem? Any help will be appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire