Laravel 迁移表字段的类型更改

2022-08-30 08:38:14

以下是我的文件2015_09_14_051851_create_orders_table.php。我想通过新的迁移作为字符串进行更改。$table->integer('category_id');

<?php

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

class CreateOrdersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('num');
            $table->integer('user_id');

            $table->text('store_name');
            $table->integer('store_name_publication');

            $table->string('postal_code', 255);
            $table->string('phone_number', 255);

            $table->text('title');
            $table->text('description');

            $table->string('list_image_filename1', 255);
            $table->string('list_image_filename2', 255)->nullable();
            $table->string('list_image_filename3', 255)->nullable();
            $table->string('list_image_filename4', 255)->nullable();
            $table->string('list_image_filename5', 255)->nullable();

            $table->integer('term');

            $table->datetime('state0_at')->nullable();
            $table->datetime('state1_at')->nullable();
            $table->datetime('state2_at')->nullable();
            $table->datetime('state3_at')->nullable();
            $table->datetime('state4_at')->nullable();
            $table->datetime('state5_at')->nullable();
            $table->datetime('state6_at')->nullable();
            $table->datetime('state7_at')->nullable();
            $table->datetime('state8_at')->nullable();
            $table->datetime('state9_at')->nullable();
            $table->datetime('state10_at')->nullable();

            $table->integer('category_id');
            $table->integer('target_customer_sex');
            $table->integer('target_customer_age');

            $table->integer('payment_order');
            $table->integer('num_comment');
            $table->integer('num_view');
            $table->string('num_pop');

            $table->integer('money');
            $table->integer('point');

            $table->datetime('closed_at');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('orders');
    }

}

答案 1

更新: 31 Oct 2018, 仍然可在 laravel 5.7 https://laravel.com/docs/5.7/migrations#modifying-columns 上使用

若要对现有数据库进行一些更改,可以在迁移中使用 来修改列类型。change()

这是你可以做的

Schema::table('orders', function ($table) {
    $table->string('category_id')->change();
});

请注意,您需要将 doctrine/dbal dependency 添加到 composer.json 以获取更多信息,您可以在此处找到它 http://laravel.com/docs/5.1/migrations#modifying-columns


答案 2

标准解决方案对我不起作用,当将类型从TEXT更改为LONGTEXT时。

我不得不这样:

public function up()
{
    DB::statement('ALTER TABLE mytable MODIFY mycolumn  LONGTEXT;');
}

public function down()
{
    DB::statement('ALTER TABLE mytable MODIFY mycolumn TEXT;');
}

这可能是一个教义问题。更多信息请点击这里

另一种方法是使用 string() 方法,并将值设置为文本类型 max length:

    Schema::table('mytable', function ($table) {
        // Will set the type to LONGTEXT.
        $table->string('mycolumn', 4294967295)->change();
    });

推荐