簡體   English   中英

Laravel 在遷移中向現有表添加新列

[英]Laravel Add a new column to existing table in a migration

我不知道如何使用 Laravel 框架向我現有的數據庫表中添加新列。

我嘗試使用...編輯遷移文件

<?php

public function up()
{
    Schema::create('users', function ($table) {
        $table->integer("paid");
    });
}

在終端中,我執行php artisan migrate:installmigrate

如何添加新列?

要創建遷移,您可以在 Artisan CLI 上使用 migrate:make 命令。 使用特定名稱以避免與現有模型發生沖突

對於 Laravel 3:

php artisan migrate:make add_paid_to_users

對於 Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users

然后,您需要使用Schema::table()方法(因為您正在訪問現有表,而不是創建新表)。 您可以添加這樣的列:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}

並且不要忘記添加回滾選項:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}

然后你可以運行你的遷移:

php artisan migrate

這在 Laravel 3 的文檔中都有很好的介紹:

對於 Laravel 4 / Laravel 5:

編輯:

使用$table->integer('paid')->after('whichever_column'); 在特定列之后添加此字段。

我將為使用 Laravel 5.1 及更高版本的未來讀者添加 mike3875 的答案。

為了使事情更快,您可以像這樣使用標志“--table”:

php artisan make:migration add_paid_to_users --table="users"

這將自動添加updown方法內容:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        //
    });
}

同樣,您可以在創建新遷移時使用--create["table_name"]選項,這將為您的遷移添加更多樣板。 小點,但在做大量工作時很有幫助!

Laravel 5.6 及以上

如果您想將新列作為外鍵添加到現有表中。

通過執行以下命令創建一個新的遷移: make:migration

例子 :

php artisan make:migration add_store_id_to_users_table --table=users

在 database/migrations 文件夾中,您有新的遷移文件,例如:

2018_08_08_093431_add_store_id_to_users_table.php (見評論)

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStoreIdToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            // You probably want to make the new column nullable
            $table->integer('store_id')->unsigned()->nullable()->after('password');

            // 2. Create foreign key constraints
            $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Drop foreign key constraints
            $table->dropForeign(['store_id']);

            // 2. Drop the column
            $table->dropColumn('store_id');
        });
    }
}

之后運行命令:

php artisan migrate

如果您出於任何原因想要撤消上次遷移,請運行以下命令:

php artisan migrate:rollback

您可以在文檔中找到有關遷移的更多信息

如果您使用的是 Laravel 5,則命令為;

php artisan make:migration add_paid_to_users

所有用於制作事物的命令(控制器、模型、遷移等)都已移至make:命令下。

php artisan migrate仍然是一樣的。

您可以在初始Schema::create方法中添加新列,如下所示:

Schema::create('users', function($table) {
    $table->integer("paied");
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});

如果您已經創建了一個表,您可以通過創建一個新的遷移並使用Schema::table方法向該表添加其他列:

Schema::table('users', function($table) {
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});

文檔對此相當詳盡,並且從version 3version 4沒有太大變化。

Laravel 7

  1. 使用 cli 命令創建遷移文件:

    php artisan make:migration add_paid_to_users_table --table=users

  2. 將在遷移文件夾中創建一個文件,在編輯器中打開它。

  3. 添加到函數 up():

Schema::table('users', function (Blueprint $table) {
    // Create new column
    // You probably want to make the new column nullable
    $table->integer('paid')->nullable()->after('status');
}
  1. 添加到函數 down(),這將在遷移因某些原因失敗時運行:

    $table->dropColumn('paid');

  2. 使用 cli 命令運行遷移:

    php artisan migrate


如果您想向表中添加一列以創建外鍵約束:

在上述過程的第 3 步中,您將使用以下代碼:

$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');

$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');

在上述過程的第 4 步中,您將使用以下代碼:

// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');

這些東西在 Laravel 5.1 上有效。

首先,在您的終端上執行此代碼

php artisan make:migration add_paid_to_users --table=users

之后轉到您的項目目錄並展開目錄數據庫 - 遷移和編輯文件 add_paid_to_users.php,添加此代碼

public function up()
{
    Schema::table('users', function (Blueprint $table) {
         $table->string('paid'); //just add this line
    });
}

之后返回您的終端並執行此命令

php artisan migrate

希望這有幫助。

在 Laravel 8

php artisan make:migration add_columnname_to_tablename_table --table=tablename

然后在創建遷移之后

public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->datatype('column_name')->nullable();
        });
    }
public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->dropColumn('column_name');
        });
    }

然后運行

php artisan migrate

如果您遇到錯誤,請使用創建表之前的日期重命名遷移名稱,然后再次運行php artisan migrate

警告這是一個破壞性的行為。 如果您使用它,請確保首先備份您的數據庫

您可以簡單地修改現有的遷移文件,例如在表中添加一列,然后在終端中輸入:

$ php artisan migrate:refresh

首先回滾之前的遷移

php artisan migrate:rollback

之后,您可以修改現有的遷移文件(添加新的、重命名或刪除列),然后重新運行您的遷移文件

php artisan migrate

將列添加到您的遷移文件並運行此命令。

php artisan migrate:refresh --path=/database/migrations/your_file_name.php

盡管正如其他人提到的那樣,遷移文件是最佳實踐,但在緊要關頭,您還可以添加一個包含 tinker 的列。

$ php artisan tinker

這是終端的單行示例:

Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })



(這里是為了可讀性而格式化的)

Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ 
    $table->integer('paid'); 
});

首先你必須創建一個遷移,你可以在 laravel artisan CLI 上使用 migrate:make 命令。像 laravel 4 這樣的舊 laravel 版本你可以在 Laravel 4 上使用這個命令:

php artisan migrate:make add_paid_to_users

而對於 Laravel 5 版本

對於 Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users

然后你需要使用 Schema::table() 。 你必須添加列:

public function up()

{

    Schema::table('users', function($table) {

        $table->integer('paid');

    });

}

你可以進一步檢查這個

如果您不想將藍圖(架構)拆分為兩個遷移文件,那么您可以做的最好的事情是從數據庫中刪除該表,然后重命名遷移文件的最后一個編號並執行

php artisan migrate

這有助於您保護其他表的數據。

運行此命令: php artisan migrate:fresh --seed 它將刪除表並重新添加它更新添加到數據庫的所有列

你可以做的是像,

Schema::create('users', function ($table) { $table->integer("paid"); });

在編寫此寫入命令php artisan migratephp artisan refresh我個人更喜歡刷新而不是新遷移,因為如果您執行新遷移,它將刪除所有數據,刷新不會。

但唯一的例外是,如果您進行刷新並且表中有任何外鍵,則它不會重新建立關系,因此您會收到類似錯誤,

無法添加外鍵約束

在 laravel 8

php artisan make:migration add_paid_to_users_table --table=users

public function up()
 {
    Schema::table('users', function($table) {

        $table->integer('paid');

  });

}

在 laravel 9

  add a new column in existing table

  php artisan make:migration add_paid_to_users_table

if you want to create new migration then do the below code

  php artisan make:migration create_users_table --create=users

只需在遷移文件中編輯要添加新列的內容並運行 -->

php工匠遷移:刷新

如果解決方案都不起作用,您可能已經重新創建了遷移文件,然后添加了一個新列並嘗試運行php artisan migrate以更新舊表,該表將嘗試創建該表,但該表已經存在,因此會出錯。 要解決將遷移文件重命名為以前命名的問題(以日期開頭),然后添加新列運行php artisan migrate ,它實際上會更新舊的而不是創建,解決了我的問題。

暫無
暫無

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

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