2

mt_rand() and random_int() both are generate only one numbe and insert it in database where the number is already exist. I want to clear that , I want to insert a random number that is not exist in database.

I took this code from here

In my case in laravel 8, the code looks like below

public function generateProductCode() {
    $number = mt_rand(1000000000, 9999999999); 

    // call the same function if the barcode exists in Dtabase already
    if ($this->productCodeExists($number)) {
        return $this->generateProductCode();
    }

    // otherwise, it's valid and can be used
    return $number;
}

public function productCodeExists($number) {
    // query the database and return a boolean
    // for instance, it might look like this in Laravel
    return Product::where('product_code', $number)->exists();
}

The model name is Product and DB table name is products. 'product_code' is the column where I want to do this query. In store function where I Call generateProductCode() function, it looks like below

public function store(Request $request)
{
    $product = new Product;
    //Generate a Product key
    $product->product_code = $this->generateProductCode();
    $product->save();
 }

It returns all time only one numbers that is 2147483647 . Photo is here What is the wrong? What is the perfect query to check exist so that it returns another that is not exist in the database.

  • 1
    use `random_int()` instead of `mt_rand()`, with same parameters – N69S Nov 03 '21 at 07:45
  • 2
    You could just use the [Uuid](https://laravel.com/docs/8.x/helpers#method-str-uuid) helper that ships with Laravel. – Peppermintology Nov 03 '21 at 07:54
  • 2
    make primary key in this model in uuid - and all other things will be automatic. – Maksim Nov 03 '21 at 08:02
  • mt_rand() and random_int() both are generate only one number. and insert it database where the number is already exist. I want to clear that , I want to insert a random number that is not exist in database. – Arafat Rahman Nov 03 '21 at 08:49
  • 2
    `2147483647` is the maximum signed integer you can have on a 32 bit system which seems like what you have. You should indeed use a UUID to generate unique identifiers and in that case you probably don't even need to check they are unique. – apokryfos Nov 03 '21 at 08:49
  • 1
    Make your field `product_code` - `BIGINT UNSIGNED` – vvpanchev Nov 03 '21 at 09:29
  • Thanks @all you all are helpful. – Arafat Rahman Nov 03 '21 at 11:42

2 Answers2

2

according to php doc:

mt_rand: This function does not generate cryptographically secure values, and should not be used for cryptographic purposes

you can use random_int

it Generates cryptographic random integers that are suitable for use where unbiased results are critical

 $number = random_int(1000000000, 9999999999); 
OMR
  • 11,736
  • 5
  • 20
  • 35
  • mt_rand() and random_int() both are generate only one number. and insert it database where the number is already exist. I want to clear that , I want to insert a random number that is not exist in database. – Arafat Rahman Nov 03 '21 at 08:49
2

You should create a recursion function check if the number is already used or not.

public function store(Request $request)
{
    $product = new Product;
    //Generate a Product key
    $product->product_code = $this->newRandomInt();
    $product->save();
 }

 private function newRandomInt()
    {
        $number = random_int(1000000000, 9999999999); 
        $isUsed =  Product::where('number', $number)->first();
        if ($isUsed) {
            return $this->newRandomInt();
        }
        return $number;
    }
Zrelli Majdi
  • 1,204
  • 2
  • 11
  • 16