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

Viewed 37

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 ?

0 Answers
Related