0

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.