Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

Consume external api and save into db using Guzzle

I am having thismodel in my Laravel-5.8 project: Employee

Employee

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');
    }    

}

That is,:

App\Employee

Also I have an external api that comes in form of JSON get request.

https://api.employees.net/allemployees

I have viewed it with postman get request and I have something like this:

{
    "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",
},

and so on... this continues

Already I have created this function:

  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);

  ...
  }

What I want to achieve is that as I get the external api using Guzzle, I want to save it into the DB:

  1. Save into employees table (App\Employee)

                 'staff_code', = StaffCode
                   'first_name', = FirstName
                   'last_name', = LastName
           'department_id', = DepartmentCode
    
  2. StaffCode is unique. So if it already exists it should only update, else it should save

How do I complete the function I have already written

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

...
}

to achieve this?

Thank you

0 likes
4 replies
MichalOravec's avatar

@noblemfd Just add ->getBody()like

$client = new GuzzleHttp\Client();

$res = $client->request('GET','https://api.employees.net/allemployees')->getBody();

$clientdatas = json_decode($res, true);
1 like
MitjaZ's avatar
MitjaZ
Best Answer
Level 5

you cloud use updateOrCreate method.

foreach($clientdatas as $clientdata) {
	App\Employee::updateOrCreate([
		'id' => $clientdata['ID'],
		'first_name' => $clientdata['FirstName'],....
	]);	
}
1 like
noblemfd's avatar

Am I adding this immediately after my code I have written before and is there any need for getBody

MitjaZ's avatar

If $clientdatas is array then no ...

You add this line after

$clientdatas = json_decode($res, true);

Please or to participate in this conversation.