Reputation: 1001
im trying to iterate in a foreach loop a json object, but with no success, it keeps giving me error regarding html entities.
The data im trying to output in my foreach is above:
Data: variable that is stored the data above is '$product->tags['data']'
{"tags":[{"type":"circle","points":[[1.0449999570846558,0.5450000166893005],[0.9850000143051147,0.4399999976158142]],"popup":{"title":"my title","description":"my description"}},{"type":"rectangle","points":[[0.03500000014901161,0.125],[0.3400000035762787,0.6000000238418579]],"popup":{"title":"roupa","description":"guardar roupa"}}]}
My code:
@foreach($product->tags['data']->tags as $tag){
{{$tag->type}}
}
My error:
Invalid argument supplied for foreach()
Upvotes: 0
Views: 4751
Reputation: 414
Assuming your json is:
$json='{"tags":[{"type":"circle","points":[[1.0449999570846558,0.5450000166893005],
[0.9850000143051147,0.4399999976158142]],
"popup":{"title":"my title","description":"my description"}},
{"type":"rectangle","points":[[0.03500000014901161,0.125],
[0.3400000035762787,0.6000000238418579]],"popup":{"title":"roupa","description":"guardar roupa"}}]}';
I assume that you are not decoding the json
when you pass it to the view, so you gotta do it in your controller:
return view('sample')->with('data',json_decode($json));
json_decode is the function to decode your json data to be iteratable in blade.
Now, in your blade template, you can loop through the tags like this:
@foreach($data->tags as $tag)
{{$tag->type}}
<br/>
@endforeach
Upvotes: 0
Reputation: 606
$product->tags['data'] = '{"tags":[{"type":"circle","points":[[1.0449999570846558,0.5450000166893005],[0.9850000143051147,0.4399999976158142]],"popup":{"title":"my title","description":"my description"}},{"type":"rectangle","points":[[0.03500000014901161,0.125],[0.3400000035762787,0.6000000238418579]],"popup":{"title":"roupa","description":"guardar roupa"}}]}';
$product->tags['data'] = json_decode($product->tags['data'], true);
foreach($product->tags['data']['tags'] as $tag)
echo $tag['type'].'<br>';
Upvotes: 1