65

Generating Auto Invoice Numbers in Laravel 5.2

 3 years ago
source link: https://www.codesd.com/item/generating-auto-invoice-numbers-in-laravel-5-2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Generating Auto Invoice Numbers in Laravel 5.2

advertisements

I am building an invoice application using laravel 5.2

I want to create automatic invoice numbers incrementing. Meaning,

If i open create invoice page, i should automatically get #0001 so when i save the invoice, under my invoice_number field it will be 0001 then when i am creating another invoice, the controller will check for the last invoice_number in the database and automatically give me #0002.

Any idea on how to do that or something similar??

Thanks


Get the last record from the db in descending order.

$invoice = new Invoice();

$lastInvoiceID = $invoice->orderBy('id', DESC)->pluck('id')->first();
$newInvoiceID = $lastInvoiceID + 1;

This will return the last ID. Then add 1 to it, and display it on the front end {{ $newInvoiceID }}.

Possible issue: What if two users are creating invoices simultaneously? Once they save, the numbers will be different if invoice number is a unique identifier.

One option is to create invoices per user.

$invoice = new Invoice();

$lastInvoiceID = $invoice->where('user_id', '=', Auth::user()->id)->orderBy('invoice_id', DESC)->pluck('invoice_id')->first();
$newInvoiceID = $lastInvoiceID + 1;


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK