PHPackages                             sirajcse/laravel-unique-id-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sirajcse/laravel-unique-id-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sirajcse/laravel-unique-id-generator
====================================

Generate custom Unique ID or Code (With Prefix or Suffix Or Both Or Only Unique Id) Or reset your ID after change of Prefix or Suffix Or Both in laravel framework

v1.0.1(4y ago)729.2k—4.1%7PHPPHP &gt;=5.6.0

Since Dec 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/SirajCse/laravel-unique-id-generator)[ Packagist](https://packagist.org/packages/sirajcse/laravel-unique-id-generator)[ RSS](/packages/sirajcse-laravel-unique-id-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

README
------

[](#readme)

> > >

\##Generate custom Unique ID or Code (With Prefix or Suffix Or Both Or Only Unique Id) Or reset your ID after change of Prefix or Suffix Or Both in laravel framework
=====================================================================================================================================================================

[](#generate-custom-unique-id-or-code-with-prefix-or-suffix-or-both-or-only-unique-id-or-reset-your-id-after-change-of-prefix-or-suffix-or-both--in-laravel-framework)

Installation
============

[](#installation)

```
composer require sirajcse/laravel-unique-id-generator

```

##### How to use it? You can use it in your controller code like as a helper or inside your model by defining a boot method.

[](#how-to-use-it-you-can-use-it-in-your-controller-code-like-as-a-helper-or-inside-your-model-by-defining-a-boot-method)

@@Unique ID/Code Generatore Like@@@
-----------------------------------

[](#unique-idcode-generatore-like)

Inv-000001/12/21

```
'table' => 'invoices' [sting table name]
'field'=>'invoice_id' [Default 'id'] [Optional][any string field name]
'length' => 12 [Integer value Id length]
'prefix'=>'Inv-' [Default ''] [Optional] [any string]
'suffix'=>date('/m/y') [Default ''] [Optional][any string]
'reset_on_change'=>false[ Default false] [Optional] [Options are 1.prefix , 2.suffix 3.both 4.false]
uniqueId=000001

```

###### Default field Default field value id; \[like 'field'=&gt;'id'\] If need 'code' field in a table; 'field'=&gt;'code'

[](#default-field-default-field-value-id-like-fieldid-if-need-code-field-in-a-table-fieldcode)

###### Prefix Or Suffix Default Value Prefix=&gt;'' or Suffix=&gt;'';

[](#prefix-or-suffix-default-value-prefix-or-suffix)

###### Reset On Change If you want to reset value at the beginning of every prefix or suffix or both changes then set it value; \[reset\_on\_change has 4 option prefix,suffix,both,false\] Default Value reset\_on\_change= false;

[](#reset-on-change-if-you-want-to-reset-value-at-the-beginning-of-every-prefix-or-suffix-or-both-changes-then-set-it-value-reset_on_change-has-4-option-prefixsuffixbothfalse-default-value-reset_on_change-false)

Example with controller
-----------------------

[](#example-with-controller)

```
Import ID generator for Prefix
use sirajcse\UniqueIdGenerator\UniqueIdGenerator;

```

### For Prefix

[](#for-prefix)

```
public function store(Request $request){
    $id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6, 'prefix' => date('y')]);
    $todo = new Todo();
    $todo->id = $id;
    $todo->title = $request->get('title');
    $todo->save();
}
// 210001 210002 220003 230004

```

### For Suffix

[](#for-suffix)

```
public function store(Request $request){
    $id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6, 'suffix' => date('y')]);
    $todo = new Todo();
    $todo->id = $id;
    $todo->title = $request->get('title');
    $todo->save();
}
// 000121 000221 000322 000422

```

### For Both Prefix and Suffix

[](#for-both-prefix-and-suffix)

```
public function store(Request $request){
    $id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 10,'prefix' => 'Inv-', 'suffix' => date('y')]);
    $todo = new Todo();
    $todo->id = $id;
    $todo->title = $request->get('title');
    $todo->save();
}
// Inv-000121 Inv-000221 Inv-000322 Inv-000422

```

### N.B: If you generate ID to the table id field then you must have to set the id field as fillable and public $incrementing = false; inside your model.

[](#nb-if-you-generate-id-to-the-table-id-field-then-you-must-have-to-set-the-id-field-as-fillable-and-public-incrementing--false-inside-your-model)

### Example with the model

[](#example-with-the-model)

In your model add a boot method. The ID will automatically generate when a new record will be added. Associative array for 'prefix', 'suffix' add if needed

```
public static function boot() {
parent::boot();
self::creating(function ($model) {
$model->uuid = UniqueIdGenerator::generate(['table' => $this->table, 'length' => 10,'prefix' =>'Inv-', 'suffix' =>date('y')]);
});
}
// Inv-000121 Inv-000221 Inv-000322 Inv-000422

```

Parameters
----------

[](#parameters)

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

```
        table = Your table name. Like 'users','invoice'
        field = Optional. By default, it works on the id field. You can set other field names also.like 'code', 'invoice_id', 'student_uuid'
        length = Your ID/Code length is total length **[ length= prefix+ unique id + suffif ]**
        prefix = Optional. By default, it prefix is empty. Define your prefix if your need. It can be a year prefix, month ,any custom letter or empty.
        suffix = Optional. By default, it suffix is empty. Define your suffix if your need. It can be a year suffix, month or any custom letter or empty
        reset_on_change = Optional, default false. If you want to reset id from 1 on prefix,suffix or both changes then set it prefix,suffix,both,false.
                    'reset_on_change'=>'prefix' Only Changes of prefix value reset
                    'reset_on_change'=>'suffix' Only Changes of suffix value reset
                    'reset_on_change'=>'both' Changes of both prefix and suffix value reset
                    'reset_on_change'=>'false' Optional. By default, it reset_on_change is false.

```

#### Example: 01

[](#example-01)

```
$config = [ 'table' => 'todos', 'length' => 6, 'prefix' => date('y') 'suffix' => '' ];
// now use it $id = UniqueIdGenerator::generate($coFnfig);

```

#### Example 02: Only unique id/code with out Prefix, Suffix

[](#example-02-only-unique-idcode-with-out-prefix-suffix)

// use within single line code

```
$id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6]);
// output: 000001, 0000002

```

#### Example 03: INV-000001 for prefix string.

[](#example-03-inv-000001-for-prefix-string)

Your field must be varchar.

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

```

#### Example 04: 000001/2021 for suffix string.

[](#example-04-0000012021-for-suffix-string)

```
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'suffix' =>date('/Y')]);
//output: 00001/2021, 00002/2021

```

#### Example 05: INV-000001/2021 for prefix,suffix (both) string.

[](#example-05-inv-0000012021-for-prefixsuffix-both-string)

```
`$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 14,'prefix' =>'INV-', 'suffix' =>date('/Y')]);
//output: INV-00001/2021, INV-00002/2021`

```

#### Example 06: By default (ID field),

[](#example-06-by-default-id-field)

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 data type.

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

```

#### Example 07: By default (reset your ID),

[](#example-07-by-default-reset-your-id)

```
This package won't reset your ID when you change your prefix,suffix or Both of ID.
If you want to reset your ID from 1 on every prefix changes then pass reset_on_change => 'prefix'
If you want to reset your ID from 1 on every suffix changes then pass reset_on_change => 'suffix'
If you want to reset your ID from 1 on every prefix and suffix (both) changes then pass reset_on_change => 'both'___

```

#### Example 08: Reset Prefix ID yearly

[](#example-08-reset-prefix-id-yearly)

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

```

#### Example 09: Reset ID monthly

[](#example-09-reset-id-monthly)

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

```

#### Example 10: Or any prefix change

[](#example-10-or-any-prefix-change)

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

```

#### Example 11: Reset Suffix ID yearly

[](#example-11-reset-suffix-id-yearly)

```
`$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'suffix' =>date('y'),'reset_on_change'=>'suffix']]);
//output: 0000000120,0000000220,0000000320
//output: 0000000121,0000000221,0000000321`

```

#### Example 12: Reset Prefix and Suffix (Both )ID yearly

[](#example-12-reset-prefix-and-suffix-both-id-yearly)

$prefix='INV'; $prefix='Pro';

```
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 13, 'prefix' =>$prefix,'suffix' =>date('y'),'reset_on_change'=>'both']]);
//output: INV0000000120,INV0000000220,INV0000000320
//output: Pro0000000121,Pro0000000221,Pro0000000321

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~137 days

Total

2

Last Release

1467d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a6ac569f3d89638a6512d65b61ee0445dc5329cda681e3d9e4af8269a276f1e7?d=identicon)[SirajCse](/maintainers/SirajCse)

---

Top Contributors

[![SirajCse](https://avatars.githubusercontent.com/u/13940491?v=4)](https://github.com/SirajCse "SirajCse (20 commits)")

### Embed Badge

![Health badge](/badges/sirajcse-laravel-unique-id-generator/health.svg)

```
[![Health](https://phpackages.com/badges/sirajcse-laravel-unique-id-generator/health.svg)](https://phpackages.com/packages/sirajcse-laravel-unique-id-generator)
```

###  Alternatives

[roxblnfk/unpoly

Remove unnecessary PHP polyfills

4117.7k5](/packages/roxblnfk-unpoly)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
