簡體   English   中英

如何更改“用戶”表中“名稱”列的名稱?

[英]How to change the name of 'name' column in the 'users' table?

我有一個帶有“用戶”表的 Laravel 應用程序。 我希望它(表)有“用戶名”列而不是“名稱”列,即這樣的結構:

ID 用戶名 email 密碼 ...ETC。
1個 用戶名 1 email1@example.net xEvehgv#52... ...
2個 用戶名 2 email2@example.net dkjbg#%RD.. ...

我怎樣才能在 Laravel 9 的幫助下做到這一點,而不會出現表單數據驗證和第三方包的問題? 先感謝您!

要將“用戶”表中的“名稱”列重命名為“用戶名”,請使用以下命令:

php artisan make:migration rename_name_to_username_in_users

這將創建一個新的遷移文件。 在文件中,您可以使用帶有renameColumn()方法的表方法:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->renameColumn('name', 'username');
    });
}

最后,運行遷移。

php artisan migrate

注意:更改用戶表中的列名不是一個好主意,因為很多包使用表的默認名稱而不是您可以添加一個包含您的客戶名稱的新列

例如

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('username');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

否則,您可以通過轉到 database/migrations/_create_users_table.php 來實現

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

而且您還運行了遷移

php artisan migrate

或者

php artisan migrate:refresh

我剛剛在這里找到了問題的答案。 謝謝大家關注這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM