Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda