简体   繁体   English

Laravel 如何生成六个不同整数的随机用户ID

[英]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.我想更改从 1 开始的当前自动递增用户 ID,并为 Laravel 中用户的每个新用户递增 1,为六个不同的随机整数,例如 414521 并且是唯一的。 I would like the six random numbers ID of a user, to be generated when the user is created, help me, please.我想在创建用户时生成用户的六个随机数ID,请帮助我。

Should I use mt_rand(0, 6) and if yes, where should I actually implement it?我应该使用 mt_rand(0, 6) 如果是,我应该在哪里实际实施它?

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用户控制器.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. ID 可以保留为 integer。 The problem lies in it having exactly six digits and still be unique, so 100000 to 999999.问题在于它正好有六位数字并且仍然是唯一的,所以从 100000 到 999999。

You need a unique seed, and if you never delete a user, that could be the number of users (1 to 899999).您需要一个唯一的种子,如果您从不删除用户,则可能是用户数(1 到 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.然后将此数字转换为从 100000 到 999999 的数字。可能可以使用 Feistel 密码的变体或 XOR 后跟确定性洗牌来完成某些操作。 However, sequential IDs will be recognizably sequential.但是,顺序 ID 将是可识别的顺序。

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.另一种方法是创建一个反向 ID 表,其中包含一个六位数的 integer X。该表包含 899999 条记录——应该是大约 4 兆字节,所以我认为这是可以接受的。 When you need an ID, you SELECT X FROM reverseID ORDER BY RAND() LIMIT 1 , then DELETE FROM reverseID where X=value .当你需要一个 ID 时,你SELECT X FROM reverseID ORDER BY RAND() LIMIT 1 ,然后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. ID 将始终为六位数,因为这是您放入 reverseID 的内容,您始终知道仍有多少 ID 可用,并且将随机选择新 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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