Skip to content

Instantly share code, notes, and snippets.

@tisuchi
Last active July 29, 2022 13:22
Show Gist options
  • Save tisuchi/a2640f494b2d341a5edac33abca3dd52 to your computer and use it in GitHub Desktop.
Save tisuchi/a2640f494b2d341a5edac33abca3dd52 to your computer and use it in GitHub Desktop.
How to generate Invoice Number with Laravel
<?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
@hotaruJtawisak
Copy link

hotaruJtawisak commented Mar 18, 2019

$invoice = new Invoice();

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

@mreduar
Copy link

mreduar commented Jul 3, 2019

This gives error the first time you are going to generate an invoice

@hgnelson83
Copy link

The code is always going to go to the if statement no matter what day it is.

@okellogabrielinnocent
Copy link

okellogabrielinnocent commented Feb 16, 2022

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