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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化