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

noblemfd's avatar

How to Dynamically implement my API

I am consuming an external API using Guzzle and saving it into database:

use App\Employee;

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

   foreach($clientdatas as $clientdata)
    {
        $employee = HrEmployee::updateOrCreate([
            'employee_code' => $clientdata['staff_id'],
        ],
        [
            'first_name'                            => $clientdata['first_name'],
            'last_name'                             => $clientdata['last_name'],
            'other_name'                            => $clientdata['middle_name'],
	'dept_code'                            => $clientdata['department_id']
        ]);  
     }
  }

Currently what I have is that if

'employee_code' => $clientdata['staff_id'],

exists, it should update

        [
            'first_name'                            => $clientdata['first_name'],
            'last_name'                             => $clientdata['last_name'],
            'other_name'                            => $clientdata['middle_name'],
	'dept_code'                            => $clientdata['department_id']
        ]);  

it should save new records.

However, I want to change it that if

'employee_code' => $clientdata['staff_id'], exists, it should update

        [
            'first_name'                            => $clientdata['first_name'],
            'last_name'                             => $clientdata['last_name'],
            'other_name'                            => $clientdata['middle_name'],
        ]);  

except

        [
	'dept_code'                            => $clientdata['department_id']
        ]);  

If not, it should save everything.

What I mean is that, if data already exists (that is if 'employee_code' => $clientdata['staff_id'],) it should not update this: 'dept_code' => $clientdata['department_id']. That means it should exclude it. But if its a new record it save 'dept_code' => $clientdata['department_id']. along with other records

How do I achieve this?

Thanks

0 likes
1 reply
guybrush_threepwood's avatar
Level 33

Hi @noblemfd

Can you select the unique record by the employee_code AND dept_code columns or is that not possible?

If so, you could change your code to:

        $employee = HrEmployee::updateOrCreate([
            'employee_code' => $clientdata['staff_id'],
    	    'dept_code' => $clientdata['department_id']
        ],
        [
            'first_name' => $clientdata['first_name'],
            'last_name' => $clientdata['last_name'],
            'other_name' => $clientdata['middle_name']
        ]);  

If not I'm afraid you're going to be a little more verbose:

$employee = HrEmployee::firstOrNew([
    'employee_code' => $clientdata['staff_id']
]);

$employee->fill([
    'first_name' => $clientdata['first_name'],
    'last_name' => $clientdata['last_name'],
    'other_name' => $clientdata['middle_name']
]);

if (! $employee->exists) {
    $employee->dept_code = $clientdata['department_id'];
}

$employee->save();

Please or to participate in this conversation.