mysql - 如何在 mysql 中使用 laravel 迁移将数据库列 'null' 更改为 'nullable'?

标签 mysql laravel migration alter-table

这是我的车辆表。我想通过迁移更改我的数据库结构


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

class CreateVehiclesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('vehicles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image')->nullable();
            $table->string('reg_no')->unique();
            $table->string('fuel_type');
            $table->string('capacity');
            $table->double('rate');
            $table->boolean('req_carrier');
            $table->date('service_date');
            $table->integer('service_freq_km');
            $table->integer('service_freq_months');
            $table->date('insurance_date');
            $table->integer('insurance_freq_months');
            $table->date('eco_date');
            $table->integer('eco_freq_months');
            $table->date('licence_date');
            $table->integer('licence_freq_months');
            $table->integer('current_company');
            $table->string('status')->default("available");
            $table->timestampsTz();

        });
    }

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

我想为这些列提供可为空的值。 1.服务日期 2.service_freq_km 3.service_freq_months

如何在 mysql 中将这些列更改为可为空?

最佳答案

您可以阅读有关 Modifying Columns 的文档.

如果你想要这些功能,你需要先安装这个包:

composer require doctrine/dbal

然后,您需要创建另一个迁移,例如:

2019_10_24_xxxxxx_change_columns_to_nullable_in_vehicles.php

<?php

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

class ChangeColumnsToNullableInVehicles extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable()->change();
            $table->integer('service_freq_km')->nullable()->change();
            $table->integer('service_freq_months')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable(false)->change();
            $table->integer('service_freq_km')->nullable(false)->change();
            $table->integer('service_freq_months')->nullable(false)->change();
        });
    }
}

关于mysql - 如何在 mysql 中使用 laravel 迁移将数据库列 'null' 更改为 'nullable'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535757/

相关文章:

php - Laravel Eloquent 限制每个用户的结果 - 日期组合

php - 在 Laravel Eloquent 中使用子查询将过滤关系的计数列添加到选择中

php - Laravel进行复杂验证的方法

java - 如何逐步将 ejb2 无状态 session bean 迁移到 ejb3?

php - INSERT SELECT 查询不起作用

php - where 子句中的未知列,但我已经验证

MySQL 通过 ODBC 5.2 插入到 bit(1) 列

python - IntegrityError : (1062, key 条目重复)

mysql - Rails 迁移文件未将我定义的所有列添加到数据库中

ruby-on-rails - 在 Rails 中转换数据库结构/数据