Laravel custom ID generator

The unique ID is an important part of our application. Someone like to generate application ID as auto incremental and someone like to generate their unique ID in a custom format. In this post, I'll show you how to generate a custom primary key or any field of your table with custom ID via Laravel ID generator package.

First, install the package in your Laravel application.

composer require haruncpi/laravel-id-generator

Import ID generator

use Haruncpi\LaravelIdGenerator\IdGenerator;

How to use?

You must pass an associative array into generate function with table, length and prefix key.

table = Your table name.

field = Optional. By default, it works on the id field. You can set other field names also.

length = Your ID length

prefix = Define your prefix. It can be year prefix, month or your any custom letter.

reset_on_prefix_change = Optional, default false. If you want to reset id from 1 on prefix change then set it true.


Example: 01

$config = [
    'table' => 'todos',
    'length' => 6,
    'prefix' => date('y')

// now use it
$id = IdGenerator::generate($config);

// use within single line code
$id = IdGenerator::generate(['table' => 'todos', 'length' => 6, 'prefix' => date('y')]);

// output: 160001


Example 02: INV-000001 for prefix string. Your field must be varchar.

$id = IdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>'INV-']);
//output: INV-000001


Example 03: YYMM000001

$id = IdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>date('ym')]);
//output: 1910000001


Example 04: By default, this package works on the ID field. You can set another field to generate an ID. Make sure your selected field must be unique and also proper datatype.

$id = IdGenerator::generate(['table' => 'products','field'=>'pid', 'length' => 6, 'prefix' =>date('P')]);
//output: P00001


Example 05: By default, this package won't reset your ID when you change your prefix of ID. If you want to reset your ID from 1 on every prefix changes then pass reset_on_prefix_change => true

Reset ID yearly

$id = IdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>date('y')]);
//output: 2000000001,2000000002,2000000003
//output: 2100000001,2100000002,2100000003

Reset ID monthly

$id = IdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>date('ym')]);
//output: 1912000001,1912000002,1912000003
//output: 2001000001,2001000002,2001000003

Or any prefix change

$id = IdGenerator::generate(['table' => 'products', 'length' => 6, 'prefix' => $prefix]);
//output: A00001,A00002,B00001,B00002


Via Eloquent Model

public static function boot()
    self::creating(function ($model) {
        $model->uuid = IdGenerator::generate(['table' => $this->table, 'length' => 10, 'prefix' =>date('ym')]);


Share on

Related Post - Latest Post

Laravel Barcode generation tutorial

Laravel API mailer

Laravel H package

Laravel Simple Filemanager

Laravel Option Framework

Laravel Simple Captcha