0

Trying to print the json data in my laravel5.6 view using controller. I got an error

Illegal string offset 'rates'

My Controller Code

 $req_url = 'https://v3.exchangerate-api.com/bulk/6eea6e74c3abe1df9de390c2/USD';
 $response_json = file_get_contents($req_url);
 $configdata4   = json_encode($response_json);
 $final_data4 = json_decode($configdata4,true);
 return view('clientlayout.main.registerdomain',compact('final_data4'));

My view page Code

@foreach($final_data4['rates'] as $value)
{{$value['USD']}}
@endforeach

Please suggest any solutions to this issue.

James Z
  • 12,209
  • 10
  • 24
  • 44
sharmila
  • 175
  • 2
  • 19

2 Answers2

2

No need to use json_encode. You are already getting as json format

 $req_url = 'https://v3.exchangerate-api.com/bulk/6eea6e74c3abe1df9de390c2/USD';
 $response_json = file_get_contents($req_url);
 $final_data4 = json_decode($response_json,true);
 return view('clientlayout.main.registerdomain',compact('final_data4'));

Live Demo

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

Your already get the response in json format, all you need to do is to decode and return it, as the following:

$req_url = 'https://v3.exchangerate-api.com/bulk/6eea6e74c3abe1df9de390c2/USD';
$response_json = file_get_contents($req_url);
$final_data4 = json_decode($response_json);
return view('clientlayout.main.registerdomain',compact('final_data4'));
Firas Rassas
  • 509
  • 4
  • 12