0

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?

Manas
  • 3,060
  • 4
  • 27
  • 55

5 Answers5

3

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.

Community
  • 1
  • 1
Saravanan Sampathkumar
  • 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
1

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.

Amit
  • 451
  • 1
  • 6
  • 19
  • in this example $numberLength is always going to be 36 right? – Manas Feb 20 '17 at 07:37
  • I modified code, can you check , it might meet your requirements – Amit Feb 20 '17 at 07:59
  • 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
1

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.

Dylan
  • 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
0

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.

Vaibhavraj Roham
  • 1,138
  • 11
  • 26
0

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!

3472948
  • 801
  • 4
  • 15