Riza Setiawan
Riza Setiawan

Reputation: 45

Make unique ID using Laravel

I want to generate a unique id in Laravel.

EX: PO-12010001

I have tried googling and the answer is using UUID but could not understand.

Upvotes: 0

Views: 6021

Answers (6)

Hamuda
Hamuda

Reputation: 11

Since it is not clear where the last 4 characters (ID of product) come from, there are 2 different outcomes based on their origin, I am assuming that "PO" is a constant in all your products:

1.If you're auto-generating the Product ID:

$id = "PO-".date('my') . substr(uniqid(), 9, 12);

date('my') will return a two-digits form of the current month and a two-digits form of the current year. uniqid() returns a unique identifier based on the current time in microseconds, the identifier is usually a mix of letters and digits, and it is usually 13 characters long, so we use substr() to only return the last 4 characters of the string.

NOTE: we are using the last characters of uniqid() because they change every millisecond.

2.If you already have a Product ID:

$id = "PO-".date('my') . $product_id;

Upvotes: 1

Harun
Harun

Reputation: 1237

You can use the Laravel ID generator.

First Install it: composer require haruncpi/laravel-id-generator

Import the class in your controller.

use Haruncpi\LaravelIdGenerator\IdGenerator;

Now simply use it

$prefix = "PO-".date("my");  
$id = IdGenerator::generate(['table' => 'your_table_name', 'length' => 11, 'prefix' =>$prefix]);

Output

PO-12010001
PO-12010002
PO-12010003
...

Upvotes: 0

Amin Eshtiaghi
Amin Eshtiaghi

Reputation: 196

it's called Human code which can be unique identify beside an Id column in db table.

$idColumn = 1;
$dateCode = date('ym');
$newHumanCode = 'PO-'.$dateCode.substr('0000'.$idColumn, -4);
return $newHumanCode;

also you can use randome number instead of use $idColumn, for example:

$idColumn  = mt_rand();

Upvotes: 0

Wiimm
Wiimm

Reputation: 3502

First, I dopn't know. So maybe it has a similar concept. But anyway, user defined+designed unique are most likely not unique.

My recommendation is to let the database create an unique id with the autoincrement feature. This is usually the only way to guarantee unique ideas in a multi tasking environment.

The, in an second step, you can create an human readable id and use it for displaying it at the suer interface. Such query can be something like:

update table set nice_id = concat("prefix-",main_id) 
where main_id = $last_inserted_id

... or any other calculation based on counting the the number of same entries since beginning of month.

There are other solutions based on try to create an nice_id, insert it into the database, and if this fails, create the next one .. and loop until successful. But simple integers created by autoincrement are more performant on queries and for keys.

Upvotes: 0

Thilina Dharmasena
Thilina Dharmasena

Reputation: 2341

When like this situation first how you want to make your ID Item Type Month Year Product ID

Get product type in your controller

if Product = PO / Service = SE

Create a New Date using PHP

 $date2 = date('Y-m-d'); 

and get date substring with your requirement and get the last id of the product table and concat all the variable and use as Unique ID.

Upvotes: 0

Qirel
Qirel

Reputation: 26450

Your ID will always be 4 digits at the end, so we can pluck those last four characters using substr(). When you increment that by one, it will lose its padding. So 0001+1=2. We therefor pad it back using str_pad() with a length of four.

$string = 'PO-12010001';
$id = substr($string, -4, 4);

$newID = $id+1;
$newID = str_pad($newID, 4, '0', STR_PAD_LEFT);

echo "PO-1201".$newID;

Upvotes: 2

Related Questions