php - Send JSON Data from Controller to View in Blade: Laravel 5.2 -


below class returns country data

class countrydata {     public function getcountries() {         return response()->json(['data' => \app\models\countrymodel::all()]);     } } 

i have following json data returned above function

http/1.0 200 ok cache-control: no-cache content-type: application/json  {     "data":[               {                   "countryid"  : 1,                   "country"    : "united states",                   "countrycode": "us"               }            ] } 

below code in controller.

$countries = (new \app\dataaccess\countrydata())->getcountries(); return view('country.list')->with('countries', json_decode($countries)); 

below code in view

@foreach($countries["data"] $country)     <tr class="odd pointer">         <td class=" ">{{$country["country"]}}</td>         <td class=" ">{{$country["countrycode"]}}</td>     </tr> @endforeach 

when type echo $countries; above text. when type echo json_decode($countries, true); shows blank. can please guide me why happening?

reason doing because data being passed blade using below code.

$countries = (new \app\dataaccess\countrydata())->getcountries(); return view('country.list')->with('countries', json_decode($countries)); 

below should controller code:

return view('country.list')->with('countries', $countries->getdata()->data);                                                            ^^^^^^^^^^^^^^^ 

but not sure if correct way fix issue. reading jsonresponse.


Comments