简体   繁体   中英

Laravel how to generate a random user ID of six different integers

please help me. I want to change the current auto-incrementing user ID that starts from 1 and gets incremented by 1 for each new user of the users in Laravel, to be six different random integers for example 414521 and to be unique. I would like the six random numbers ID of a user, to be generated when the user is created, help me, please.

Should I use mt_rand(0, 6) and if yes, where should I actually implement it?

MySQL Users Table
+-------------------+-----------------+------+-----+---------+----------------+
| Field             | Type            | Null | Key | Default | Extra          |
+-------------------+-----------------+------+-----+---------+----------------+
| id                | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)    | NO   |     | NULL    |                |
| email             | varchar(255)    | NO   | UNI | NULL    |                |
| role              | varchar(255)    | NO   |     | user    |                |
| isActive          | tinyint         | NO   |     | 1       |                |
| email_verified_at | timestamp       | YES  |     | NULL    |                |
| password          | varchar(255)    | NO   |     | NULL    |                |
| remember_token    | varchar(100)    | YES  |     | NULL    |                |
| created_at        | timestamp       | YES  |     | NULL    |                |
| updated_at        | timestamp       | YES  |     | NULL    |                |
| status            | tinyint(1)      | NO   |     | NULL    |                |
+-------------------+-----------------+------+-----+---------+---------------

UsersController.php

  public function store(Request $request){
       $request ->validate([

        'name' => 'required|min:6',
        'email' => 'required',
           'role'=>'required',
           'password' => '',
           'confirm_password' => '',
       ]);


migrations file of users

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('email')->unique();
            $table->string('role')->default('user');
            $table->boolean('status');
            $table->tinyInteger('isActive') -> default(1);
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

The ID can remain an integer. The problem lies in it having exactly six digits and still be unique, so 100000 to 999999.

You need a unique seed, and if you never delete a user, that could be the number of users (1 to 899999). You then transform this number into a number from 100000 to 999999. Possibly something could be done using a variation of the Feistel cipher, or a XOR followed by a deterministic shuffle. However, sequential IDs will be recognizably sequential.

A different approach would be to create a reverse ID table containing a six-digit integer X. The table holds 899999 records - that should be around 4 megabytes, so I think that's acceptable. When you need an ID, you SELECT X FROM reverseID ORDER BY RAND() LIMIT 1 , then DELETE FROM reverseID where X=value . The ID will always be six digits since that's what you put into reverseID, you always know how many IDs are still available, and new IDs will be chosen at random.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM