mysql - 如何在mysql中使用laravel迁移将数据库列'null'更改为'nullable'?
问题描述
这是我的车辆表。我想通过使用迁移来更改我的数据库结构
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.service_date 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();
});
}
}
推荐阅读
- regex - 为什么 ((.+)) 捕获我的内容两次?
- wordpress - htaccess 规则将 404 重定向到 301?
- c++ - C++ 常量参考
- android - 为什么导航弹出后重新加载页面
- java - 在 Java 中以无限精度实现 BigIntegers
- python - 如果只有 int 1 是 [1, 2.3, 'blabla'] 中的列表项,为什么 Python ''in operator'' 会为 float 1.0 返回 'True'?
- jquery - 动态旋转文本和 URL
- c++ - 现代 OpenGL ID 的工作原理是什么?
- php - 在php中查找两个日期之间的假期
- svn - 如何在 SVN 中隐藏用户的路径