panoply panoply - 1 year ago 66
JSON Question

Looping through mysql json data

Working with Laravel 5.3 and MySql 5.7 using

column type. I am not able to loop through the saved Json in my view. Here is how my setup looks:

Saved JSON in

["stores", "website"]

Retrieving this JSON with Eloquent:

$navigation = Merchant::select('merchants')
->where('user_id', Auth::user()->id)

return View::make('dashboard', compact('navigation'));

Lastly my View:

@foreach($navigation as $key => $value)
<li><a href="#">{{$value->merchants}}</a></li>

The results don't loop through the saved
instead it just displays the actual JSON saved, eg:
["stores", "website"]
however I want to loop this so it would list each value from the json array, eg:



See my attached dump:


Answer Source

You need to cast it in the model.

protected $casts = [
    'merchants' => 'json'


You may need to change in your blade

@foreach($navigation as $value)
  @foreach($value->merchants as $merchant)
    <li><a href="#">{{$merchant}}</a></li>