$upsertData */ public function upsertEmployees(array $upsertData): void { $now = CarbonImmutable::now()->format('Y-m-d H:i:s'); $mappedData = array_map(static function (EmployeeUpsertData $data) use ($now): array { return [ 'matricule' => $data->matricule->value, 'nom' => $data->lastname, 'prenom' => $data->firstname, 'etablissement' => $data->etablissement->value, 'service' => $data->service->value, 'site' => $data->site->value, 'start_date' => $data->startDate->format('Y-m-d H:i:s'), 'end_date' => $data->endDate?->format('Y-m-d H:i:s'), 'code' => $data->code->value, 'last_sync' => $now, ]; }, $upsertData); // NOTE: chunking is essential here because the query builder has a binding limit of 65535 // each row has 10 bindings, so we can insert at most 65535 / 10 = 6553 rows at once // we use 1000 to be safe foreach (array_chunk($mappedData, 1000) as $chunk) { Employee::query()->upsert( values: $chunk, uniqueBy: ['matricule'], update: ['nom', 'prenom', 'etablissement', 'service', 'site', 'start_date', 'end_date', 'last_sync'] ); } } /** * @param \Domain\Models\ValueObjects\Matricule[] $matriculesToInsert * @return object{insertedCount: int, updatedCount: int, matriculesInserted: \Domain\Models\ValueObjects\Matricule[]} */ public function getImportedAfter(array $matriculesToInsert, CarbonImmutable $importStartTime): object { $employees = Employee::query() ->whereIn('matricule', $matriculesToInsert) ->get(['matricule', 'created_at', 'updated_at']); /** @var list<\Domain\Models\ValueObjects\Matricule> $matriculesInserted */ $matriculesInserted = $employees ->pluck('matricule') ->values() ->toArray(); $insertedCount = $employees ->where('created_at', '>=', $importStartTime) ->count(); $updatedCount = $employees ->where('created_at', '<', $importStartTime) ->where('updated_at', '>=', $importStartTime) ->count(); return (object) [ 'insertedCount' => $insertedCount, 'updatedCount' => $updatedCount, 'matriculesInserted' => $matriculesInserted, ]; } }