Laravel Invoices: Auto-Generate Serial Numbers - 4 Different Ways

Tutorial last revisioned on March 17, 2024 with Laravel 11

When working with invoices, you need to deal with serial numbers that look like ABC-000001. Do you know how to auto-generate them in Laravel? This tutorial will cover a few ways to do this.

DB Structure

For our example, we will use a simple invoice DB table with the following columns:

  • id
  • user_id
  • due_date
  • amount
  • serial - Full serial number like ABC-1
  • serial_number - Serial number like 1
  • serial_series - Serial series like ABC

Here's how that looks in our migration:


Schema::create('invoices', function (Blueprint $table) {

This makes our Model look like this:


use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Invoice extends Model
use HasFactory;
protected $fillable = [
protected function amount(): Attribute
return Attribute::make(
get: fn($value) => $value / 100,
set: fn($value) => $value * 100,
public function user(): BelongsTo
return $this->belongsTo(User::class);

Create Invoice: Form

Let's create the Controller methods to save a new invoice and auto-generate the serial number.

This is the form, with series options coming from the config.


use App\Http\Requests\StoreInvoiceRequest;
use App\Models\Invoice;
use App\Models\User;
class InvoiceController extends Controller
public function create()
$users = User::pluck('name', 'id');
$invoiceSeries = config('invoiceSettings.availableInvoiceSeries');
return view('invoices.create', [
'users' => $users,
'invoiceSeries' => $invoiceSeries,

These are the config values:


return [
'availableInvoiceSeries' => [

And here's the form in...

The full tutorial [10 mins, 1931 words] is only for Premium Members

Login Or Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 79 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials