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

red_wine's avatar

How to generate invoice number?

I want to generate an incremental invoice number with a prefix using the user's branch eg. ABC00001 but I don't know how. I am currently using the date and time as an invoice number. I am on Laravel 9.

private function generateInvoiceNumber() { $branch_code = Auth::user()->branch; $currentDateTime = Carbon::now(); $formattedDateTime = $currentDateTime->format('YmdHis'); $invoiceNumber = $branch_code . $formattedDateTime;

    return $invoiceNumber;
}
0 likes
4 replies
jlrdw's avatar

Just FYI, there are several good past post on this where myself and others have given examples.

But you can get the id of the new record and build off of that.

Geoffrey D's avatar

Hi,

You could say in your "invoice" model, the public string property $prefix, as if you were adding a new field to your database. On the other hand, on the admin side, you can put a select for the choice.

class Invoice {
		public string  $prefix = "ABC_"
}

In seeders, add the prefix field to generate them. Or create an enum for each type of invoice, for example :

Enums Invoice {
		case water = 'water_';
		case eat = 'eat_'
}

Please note that the articles may be out of date.

https://dev.to/mirmayne/implementing-an-invoice-numbering-system-with-php-using-laravel-6-dd4

https://laravel-news.com/enum-helpers-for-php

https://laracasts.com/discuss/channels/laravel/how-to-generate-invoice-number

Snapey's avatar

is it ok for two branches to have the same sequential part, eg ABC001234 and DEF001234

martinbean's avatar

@red_wine The first question would be, can this prefix be changed? If yes, how does that affect numbering?

For example, if the prefix is “ABC” and invoices ABC-0001, ABC-0002, and ABC-0003 are generated; what would happen if the prefix was then changed to “XYZ”? Should the numbering continue, i.e. XYZ-0004, XYZ-0005, etc? Or should numbering start over again with the new prefix (XYZ-0001, etc).

Please or to participate in this conversation.