i have json code in Laravel, how to parse in view blade?
my code :
$latitude1 = -7.325374900000001;
$longitude1 = 108.2251681;
$latitude2 = -7.331842799999999;
$longitude2 = 108.2237382;
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude1.",".$longitude1."&destinations=".$latitude2.",".$longitude2."&key=".key);
$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][1]['elements'][1]['distance']['text'];
return view('maps', compact('maps','nilaiJarak'));
i want to only get distance on laravel view blade.
but you have parsed it already in blade you can use @php
@endphp
tags for example:
@php
$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][1]['elements'][1]['distance']['text'];
@endphp
but you have to pass in blade data
not nilaiJarak
return view('maps', compact('maps', $data));
To get the distance you need:
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
or:
$json = json_decode($dataJson);
$nilaiJarak = $json->rows[0]->elements[0]->distance->text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.