I am trying to convert the JSON from a API call to another Laravel website into an eloquent model to save the data to the local web server. I am having a problem converting the JSON requested from the master server to an eloquent save method. Here is the current code I have.
public function add(Request $request)
{
// lets request the airport identifier from the central database
$client = new Client();
$res = $client->request('GET', 'http://new.fsvaos.net/api/central/airports', [
'query' => [
'icao' => $request->icao,
]
])->getBody();
// Convert the JSON to something good for Eloquent
$data = json_decode($res, true);
$airport = new Airport();
//return $data;
$airport->id = $data->id;
$airport->name = $data->name;
$airport->icao = $data->icao;
$airport->lat = $data->lat;
$airport->lng = $data->lon;
$airport->hub = 0;
$airport->fuelprice = 0;
$airport->save();
}
What exactly am I doing wrong?
EDIT: Here is the JSON response from the master server I want to add to the local database.
{
"id": 3682,
"name": "Hartsfield Jackson Atlanta Intl",
"city": "Atlanta",
"country": "United States",
"iata": "ATL",
"icao": "KATL",
"lat": "33.636719000000000",
"lon": "-84.428067000000000",
"alt": "1026",
"timezone": "-5.00",
"daylightsavings": "A",
"tz": "America/New_York"
}
Here is also the data dump from the array $data
array:1 [▼
0 => array:12 [▼
"id" => 3682
"name" => "Hartsfield Jackson Atlanta Intl"
"city" => "Atlanta"
"country" => "United States"
"iata" => "ATL"
"icao" => "KATL"
"lat" => "33.636719000000000"
"lon" => "-84.428067000000000"
"alt" => "1026"
"timezone" => "-5.00"
"daylightsavings" => "A"
"tz" => "America/New_York"
]
]
If anyone wants to try out the api for themselves to get the JSON response, just do a GET to http://new.fsvaos.net/api/central/airports?icao=[AIRPORT ICAO] and you will get any airport data from around the world.