Danu Akbar Danu Akbar - 6 months ago 372
JSON Question

Laravel : How do I parse this json data in view blade?

Hi i have JSon data like this:

[{label":a, "value":{"name":"abc","address"="New York"}}]


I've success print json with

{!! $mydata !!}


but when i try use foreach

@foreach($mydata['value'] as $value)
Member ID: {{ $value['name'] }}
@endforeach


The laravel return

ErrorException in Collection.php line 1108: Undefined index:value


I've followed this : Laravel: How do I parse this json data in view blade?

Anyone can help?

Note: The JSON and Variable is not real mine. I was changed it to make it simple.

EDITED:

added
{{ dd(json_decode($tagihan, true)) }}
and return

Result after added {{ dd(json_decode($tagihan, true)) }}

EDITED:

This my JSON:

array:5 [▼
0 => array:7 [▼
"id" => 8
"no_rek_pelanggan" => 11115
"keadaan_meteran" => 0
"angka_meteran" => 3000
"created_at" => "2016-05-28 15:28:17"
"updated_at" => "2016-05-28 15:28:17"
"all_pelanggan" => array:7 [▼
"no_rek" => 11115
"nama" => "Wicak"
"alamat" => "Malang"
"long" => 112.6153447
"lat" => -7.9528411
"created_at" => "2016-05-26 00:00:00"
"updated_at" => "2016-05-26 05:00:00"
]
]


I want to read the value from nama, alamat, long, lat in all_pelanggan. How ca i do that?

Answer

If you really want to use json encoded data in a view, use json_decode() in foreach:

@foreach(json_decode($mydata, true) as $value)
    Member ID: {{ $value['all_pelanggan']['name'] }}   
@endforeach