Last active
July 29, 2022 13:22
-
-
Save tisuchi/a2640f494b2d341a5edac33abca3dd52 to your computer and use it in GitHub Desktop.
How to generate Invoice Number with Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//IT SHOULD BE INSIDE YOUR MEHTOD | |
//get last record | |
$record = RecordModel::latest()->first(); | |
$expNum = explode('-', $record->invoiceno); | |
//check first day in a year | |
if ( date('l',strtotime(date('Y-01-01'))) ){ | |
$nextInvoiceNumber = date('Y').'-0001'; | |
} else { | |
//increase 1 with last invoice number | |
$nextInvoiceNumber = $expNum[0].'-'. $expNum[1]+1; | |
} | |
//Now add New record into database |
This gives error the first time you are going to generate an invoice
The code is always going to go to the if statement no matter what day it is.
if ($record == null or $record == "") {
if (date('l', strtotime(date('Y-01-01')))) {
$invoiceno = date('Y') . '-0001';
}
} else {
$expNum = explode('-', $record->invoiceno);
$innoumber = ($expNum[1] + 1);
$invoiceno = $expNum[0] . '-' . sprintf('%04d', $innoumber);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$invoice = new Invoice();
$lastInvoiceID = $invoice->orderBy('id', DESC)->pluck('id')->first();
$newInvoiceID = $lastInvoiceID + 1;