How to generate unique random value for each user in laravel and add it to database

前端 未结 8 1907
刺人心
刺人心 2020-12-29 08:44

I am developing a event organization website. Here when the user registers for an event he will be given a unique random number(10 digit), which we use to generate a barcode

8条回答
  •  囚心锁ツ
    2020-12-29 09:01

     $id]);
                    foreach ($whereParams as $param) {
                        $query->where(...$param);
                    }
                    if (!$query->first()) {
                        return $id;
                    }
                }
            } catch (\Throwable $e) {
                $exceptionMessage = $e->getMessage();
            }
    
            throw new GeneratorException($exceptionMessage);
        }
    
        protected static function IDGenerator(): ?\Generator
        {
            for ($i = 1; $i <= self::$limitIterations; $i++) {
                yield (string)random_int(1000000000, 9999999999);            
            }
            return null;
        }
    }
    

    sample usage

    $card->number = GeneratorHelper::generateID(Card::class, 'number');
    

提交回复
热议问题