@noblemfd Just add ->getBody()like
$client = new GuzzleHttp\Client();
$res = $client->request('GET','https://api.employees.net/allemployees')->getBody();
$clientdatas = json_decode($res, true);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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:
Save into employees table (App\Employee)
'staff_code', = StaffCode
'first_name', = FirstName
'last_name', = LastName
'department_id', = DepartmentCode
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
you cloud use updateOrCreate method.
foreach($clientdatas as $clientdata) {
App\Employee::updateOrCreate([
'id' => $clientdata['ID'],
'first_name' => $clientdata['FirstName'],....
]);
}
Please or to participate in this conversation.