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

236
Vistas
Is there a way to make an autoincrement field that depends a foreign key in laravel migrations

I'm using postgres 12 as DB.

I have an invoice table, with user_id FK, and a date.

I would like to have a autoincrement field that depends on the FK and the date. For instance

year: 2019,user_id: 2,incremental: 1
year: 2019,user_id: 2,incremental: 2
year: 2020,user_id: 2,incremental: 1 // Because year changes, incremental start to 1 again
year: 2020,user_id: 2,incremental: 2
year: 2020,user_id: 2,incremental: 3 
year: 2020,user_id: 3,incremental: 1 // Because user_id changes, incremental start to 1 again
year: 2020,user_id: 3,incremental: 2
year: 2020,user_id: 3,incremental: 3
year: 2020,user_id: 4,incremental: 4

Is there a way to achieve that directly with migrations, or should I code the logic in the code ?

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