I am creating users and saving it in my database table. I would like to create a column named: haskey, where i store a random and unique 15 digit alphanumeric string for each created user so that it is unique for every user. how do i do this?
5 Answers
Laravel has inbuilt helper function called str_random(). You could use that to generate key.
str_random(15)
Or I would suggest you to use laravel hashids.
Laravel Hashids
Require this package, with Composer, in the root directory of your project.
$ composer require vinkla/hashids
Add the service provider to config/app.php in the providers array.
Vinkla\Hashids\HashidsServiceProvider::class
If you want you can use the facade. Add the reference in config/app.php to your aliases array.
'Hashids' => Vinkla\Hashids\Facades\Hashids::class
Usage
// You can alias this in config/app.php.
use Vinkla\Hashids\Facades\Hashids;
Hashids::encode(1);
// We're done here - how easy was that, it just works!
Hashids::decode('doyouthinkthatsairyourebreathingnow');
// This example is simple and there are far more methods available.

- 1
- 1

- 3,201
- 1
- 20
- 46
-
thxx @Saravanan Sampathkumar , first method is short and simple with less code..but if i use that i shud still check if string exists in database before inserting right? – Manas Feb 20 '17 at 07:40
-
Most of the cases it should be unique. But I suggest you to use hashids. Hashids are unique. Else check and use scott's answer here http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string – Saravanan Sampathkumar Feb 20 '17 at 07:50
PHP has rand()
function generateRandomNumber($len = 16) {
$char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$randomNumber = '';
for ($i = 0; $i < $len; $i++) {
$randomNumber .= $char[rand(0, $len - 1)];
}
return $randomNumber;
}
Randomly generated value is unique.

- 451
- 1
- 6
- 19
-
-
-
thank you for that..it looks good..no matter which method i use..i must check my db if the string exist first for double checking right? though the chances are very low to generate a same string? – Manas Feb 20 '17 at 08:14
Try this:
<?php
$serial = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'0123456789');
$haskey = '';
foreach (array_rand($serial, 15) as $a) $haskey.= $serial[$a];
echo $haskey;
?>
Insert the '$haskey' to database. Though it is not guaranteed not to repeat the characters.

- 129
- 2
- 16
-
thx for your help very much appreciated..yes if i go with this method i need to double check if such string exist in the database..well i think no matter which method i go with it is still better to check if it exists right – Manas Feb 20 '17 at 07:38
You can use a UUID Package which will create ID according to the RFC 4122 standard which will be unique for each user. (Universally Unique)
Step 1 : Require the package according to your laravel version (Below steps are for Laravel 5.*)
composer require "webpatser/laravel-uuid:2.*"
Step 2 : Edit config/app.php
and add the alias.
'aliases' => [
'Uuid' => Webpatser\Uuid\Uuid::class,
]
Step 3 : Now you can generate UUID as below
Uuid::generate()
Hope it helps you.

- 1,138
- 11
- 26
Since you tagged this Laravel I'll give you a Laravel solution. You can easily generate a more truly "random" alpha-numeric string using this function:
use Illuminate\Support\Str;
$var = Str::random(15);
// $var => "kAQUxlywesTKLrP"
Good luck!

- 801
- 4
- 15