dpxo13079 2016-07-30 06:58
浏览 89
已采纳

转换Guzzle要求JSON为Eloquent

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.

  • 写回答

1条回答 默认 最新

  • dongqi0644 2016-07-30 18:32
    关注

    Now that you have added your output, it is clear. You have an array with a subarray, not an object. Change your Airport object assignments to the following

    $airport->id = $data[0]['id'];
    $airport->name = $data[0]['name'];
    $airport->icao = $data[0]['icao'];
    $airport->lat = $data[0]['lat'];
    $airport->lng = $data[0]['lon'];
    $airport->hub = 0;
    $airport->fuelprice = 0;
    
    $airport->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)