Laravel迁移表字段的类型更改

nai*_*tut 48 php migration laravel

以下是我的文件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');
    }

}
Run Code Online (Sandbox Code Playgroud)

min*_*noz 87

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

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

这就是你能做的

Schema::table('orders', function ($table) {
    $table->string('category_id')->change();
});
Run Code Online (Sandbox Code Playgroud)

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


Fra*_*les 41

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

我不得不这样:

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

public function down()
{
    DB::statement('ALTER TABLE mytable MODIFY mycolumn TEXT;');
}
Run Code Online (Sandbox Code Playgroud)

这可能是一个学说问题.更多信息在这里.

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

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

  • 这对DECIMAL列特别有用. (2认同)
  • 我可以证实这种行为.为了将列从text更改为mediumtext,我必须这样做:$ table-> string('messages',16777215) - > nullable() - > change(); (2认同)

Bla*_*nka 10

2018解决方案,其他答案仍然有效,但您不需要使用任何依赖项

首先,您必须创建一个新的迁移:

php artisan make:migration change_appointment_time_column_type
Run Code Online (Sandbox Code Playgroud)

然后在该迁移文件中up(),尝试:

    Schema::table('appointments', function ($table) {
        $table->string('time')->change();
    });
Run Code Online (Sandbox Code Playgroud)

如果不更改大小,默认varchar(191)值为,但是如果您想更改字段的大小,请执行以下操作:

    Schema::table('appointments', function ($table) {
        $table->string('time', 40)->change();
    });
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式迁移文件:

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

来自doc的更多信息

  • 你仍然需要`doctrine/dbal`。 (4认同)
  • 你还不需要使用“doctrine/dbal”依赖吗? (2认同)

Hus*_*dil 7

所有其他答案都是正确的,但是 在您运行之前

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

确保您先运行此代码

composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)

避免这个错误

RuntimeException:更改表“ items”的列需要Doctrine DBAL;安装“教义/ dbal”。


小智 7

首先作曲家需要doctrine/dbal,然后:

$table->longText('column_name')->change();
Run Code Online (Sandbox Code Playgroud)