Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

311
Vistas
Laravel - How to resolve auto-increment skipping id one step at a point in Cron jobs

I have a Laravel-5.8 project that runs cron job schedule

public function handle()
{       
    $client = new Client();
    $res = $client->request('GET','https://api.employees.net/allemployees', [
       'query' => ['key' => 'dfffgggggffffff']
   ])->getBody();

    $clientdatas = json_decode($res->getContents(), true);        

    foreach($clientdatas as $clientdata)
    {         
        Employee::updateOrCreate([
            'employee_code' => $clientdata->staff_id,
        ],
        [
            'first_name'=> $clientdata->first_name,
            'last_name'=> $clientdata->flast_name,
            'other_name'=> $clientdata->middle_name,
        ]);  

        $user = User::updateOrCreate([
            'email'=> $employee->email,
        ],
        [
            'first_name'=> $employee->first_name,
            'last_name'=> $employee->last_name,
        ]);          

         $employee->update(['user_id' => $user->id]);            
    }           
}

The id for each model are auto-increment.

I observed that while the cron job was saving the external data from the api into the mysql database, the auto-increment for Employee model skipped a step at a point. It skipped 97 and moved to 99. However, the User model is okay, it didn't skip.

I observed that it skipped a particular on that the a particular field is empty:

 'last_name'                             => $employee->last_name,

That is last_name

employee table

mysql employee table

How do I resolve it?

Thank you.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda