Laravel-如何使用Guzzle使用外部api并将其刷新到db中

Laravel-如何使用Guzzle使用外部api并将其刷新到db中,laravel,guzzle,Laravel,Guzzle,我在我的Laravel-5.8项目中使用了此模型: 雇员 class Employee extends Model { protected $table = 'employees'; protected $primaryKey = 'id'; protected $fillable = [ 'staff_code', 'first_name', 'last_name',

我在我的Laravel-5.8项目中使用了此模型:

雇员

class Employee extends Model
{
   protected $table = 'employees';

   protected $primaryKey = 'id';

   protected $fillable = [
              'staff_code',
              'first_name',
              'last_name',
              'department_id',
          ];

  public function department()
  {
     return $this->belongsTo('App\Department','department_id');
  }    

}
就是,

应用程序\员工

我还有一个外部api,它以JSON get请求的形式出现

https://api.employees.net/allemployees
我已经通过邮递员get请求查看了它,我有如下内容:

 {
    "ID": "1",
    "StaffCode": "STC001",
    "FirstName": "Japheth",
    "LastName": "Shalom",
    "DepartmentCode": "dep2",
 },
 {
    "ID": "2",
    "StaffCode": "STC002",
   "FirstName": "Ahitophel",
   "last_name": "Nedum",
   "DepartmentCode": "dep1",
},
{
    "ID": "3",
    "StaffCode": "STC003",
    "FirstName": "Joash",
    "FirstName": "Nathan",
    "DepartmentCode": "dep2",
 },
等等。。。这种情况还在继续

我已经创建了此函数:

 use App\Employee;
 use App\Department;

 public function index() 
 {  
    $client = new GuzzleHttp\Client();
    $res = $client->request('GET','https://api.employees.net/allemployees');
    $clientdatas = json_decode($res, true);

   ...
 }
除了唯一的id和staff_代码外,其他任何字段也可以从源代码更改

因为任何字段都可以随时更改。如何刷新整个数据库、保存新数据和更新更改


谢谢

我可以这样做:

 {
    "ID": "1",
    "StaffCode": "STC001",
    "FirstName": "Japheth",
    "LastName": "Shalom",
    "DepartmentCode": "dep2",
 },
 {
    "ID": "2",
    "StaffCode": "STC002",
   "FirstName": "Ahitophel",
   "last_name": "Nedum",
   "DepartmentCode": "dep1",
},
{
    "ID": "3",
    "StaffCode": "STC003",
    "FirstName": "Joash",
    "FirstName": "Nathan",
    "DepartmentCode": "dep2",
 },
使用App\Employee;
使用App\部门;
公共职能指数()
{  
$client=new GuzzleHttp\client();
$res=$client->request('GET','https://api.employees.net/allemployees');
$clientdatas=json_decode($res->getBody()->getContents(),true);
foreach($clientdatas作为$clientdata)
{
$employee=employee::firstOrNew(['id'=>$clientdata['id']]);
$employee->staff_code=$clientdata['StaffCode'];
$employee->first_name=$clientdata['FirstName'];
$employee->last_name=$clientdata['LastName'];
$employee->save();
}
}

每次进行API调用时,创建员工模型的实例,或者如果ID存在,则获取关联的模型。然后指定新值并保存模型。这样,您就可以在一个循环中创建或更新模型,而无需任何复杂操作。

不知道它是否有效,但您可以保留逻辑。。在另一个答案中,他在一个坏循环下查询

希望它能对你有所帮助

public function index() {  
    $client = new GuzzleHttp\Client();
    $res = $client->request('GET','https://api.employees.net/allemployees');
    $clientdatas = json_decode($res->getBody()->getContents(), true);
    // create a blank array for insert
    $insert_arr = [];
    foreach($clientdatas as $clientdata) {
        $make_array = [
            'id' => $clientdata['ID'],
            'staff_code' => $clientdata['StaffCode'],
            'first_name' => $clientdata['FirstName'],
            .
            .
        ];
        array_push($insert_arr, $make_array);
        // Now the the $insert_arr will ready for insert
    }
    // Here you have to check its duplicate or not  
    $exist_in_db = Employee::all('id')->toArray(); // get the id array from db

    // do the stuff for unset the duplicate 
    $final_id = array_map(function($value) {
        return $value['id'];
    }, $team_id);
    unset($exist_in_db);
    if(count($final_id) > 0){
        foreach ($insert_arr as $key => $value) {
            if (in_array($value['id'], $final_id)) {
                unset($insert_arr[$key]);
            } else {
                array_push($final_id, $value['id']);
            }
        }
    }

    // finally insert it here
    if (count($insert_arr) > 0) {
        Employee::insert($insert_arr);    
    }
}

让我知道它是否有用

一种方法是清除表并插入新记录。另一种方法是按ID检查所有本地记录,并检查它们的属性是否已从API更改。