Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Generate serial number with current year and last id automatically laravel

Guys iam working with a project. i have a small doubt,

i have a fee payment form. the bill number must be automatically generated. how to do that?

year + serial Number in this order i need the bill number.

say for example,

Bill19-20-01
Bill19-20-02
Bill19-20-03

Here ```Bill``` is predefiend texr ```19-20``` is year and ```01,02, 03``` are comes from the next id of that payment table. how to get that??

FeePayment is my model name and feepayment is my table name

even i have a drop down in all form. for accademic year.

AccademicYear my model name and accademicyear table name with columns

id      accname
1       2018-2019
2       2019-2020

How to get this?

0 likes
3 replies
alanholmes's avatar

If it requires the id as part of the reference, I would look to set this using the created event fired by eloquent when a record is created, and update the reference then (so it will need to start as a null value).

https://laravel.com/docs/6.x/eloquent#events (or observers https://laravel.com/docs/6.x/eloquent#observers)

so in your event handler, you would just do something like:

$record->update([
    'serial_number' => 'Bill19-20-' . $record->id
])

And the 19-20 you would just need to add based on the dropdown selected.

1 like
rodrigo.pedra's avatar

HI @abdulbazith ,

Came here from your comment in https://laracasts.com/discuss/channels/laravel/comparison-of-two-tables-with-a-name-in-where-condition-in-laravel

As you need the generated ID in the payment hashed ID using listeners or observers, as suggested by @alanholmes , as you will have access to the generated ID already saved and then can use it to build your hashed ID.

Take a look at the docs linked by him and if you need further help, please let us know.

Please or to participate in this conversation.