samedi 26 mars 2016

Data shows null when sending json decoded data in Blade: Laravel 5.2

Below is the class that returns Country data

class CountryData {
    public function GetCountries() {
        return response()->json(['Data' => \App\Models\CountryModel::all()]);
    }
}

I have following Json Data returned by above function

HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json 
{
    "Data":[
              {
                  "CountryID"  : 1,
                  "Country"    : "United States",
                  "CountryCode": "US"
              }
           ]
}

Below is the code in Controller.

$Countries = (new \App\DataAccess\CountryData())->GetCountries();
return view('Country.List')->with('Countries', json_decode($Countries));

Below is the code in View

@foreach($Countries["Data"] as $Country)
    <tr class="odd pointer">
        <td class=" ">{{$Country["Country"]}}</td>
        <td class=" ">{{$Country["CountryCode"]}}</td>
    </tr>
@endforeach

When I type echo $Countries; I get above text. When I type echo json_decode($Countries, true); It shows blank. Can you please guide me why this is happening?

Reason I am doing this because data is being passed into Blade using below code.

$Countries = (new \App\DataAccess\CountryData())->GetCountries();
return view('Country.List')->with('Countries', json_decode($Countries));



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire