如何使用 mysql 中的 laravel 迁移将数据库列 'null' 更改为 'nullable'?
How to change database column 'null' to 'nullable' using laravel migration in mysql?
这是我的车辆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.service_date
2.service_freq_km
3.service_freq_months
如何在 mysql 中将这些列更改为可为空?
为 alter table 创建新的迁移 class 并使用这个 up 函数
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
Schema::table 用于改变 table 和 Schema::create 用于创建新的 table
您需要创建名为“2019_10_24_00000_update_vehicle_tables”的新迁移文件
if(Schema::hasTable('vehicles')) {
Schema::table('vehicles', function($table)
{
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
安装包以更新表格composer require doctrine/dbal
由于您已经迁移了迁移文件,现在您需要使用 artisan 命令创建一个新的迁移文件:
php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles
在新建的迁移文件中,添加如下代码
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();
});
}
//对于phpartisan向下
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();
});
}
现在可以执行迁移命令了
php artisan migrate
您可以阅读有关 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();
});
}
}
$table->string('first_name')->default('DEFAULT');
如果默认值应该为 null,请将其设为可为 null。
$table->string('name')->nullable();
$table->string('name')->nullable()->default('NULL');
$table->string('name')->nullable()->default(NULL);
$table->string('name')->nullable()->default();
这是我的车辆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.service_date 2.service_freq_km 3.service_freq_months
如何在 mysql 中将这些列更改为可为空?
为 alter table 创建新的迁移 class 并使用这个 up 函数
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
Schema::table 用于改变 table 和 Schema::create 用于创建新的 table
您需要创建名为“2019_10_24_00000_update_vehicle_tables”的新迁移文件
if(Schema::hasTable('vehicles')) {
Schema::table('vehicles', function($table)
{
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
安装包以更新表格composer require doctrine/dbal
由于您已经迁移了迁移文件,现在您需要使用 artisan 命令创建一个新的迁移文件:
php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles
在新建的迁移文件中,添加如下代码
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();
});
}
//对于phpartisan向下
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();
});
}
现在可以执行迁移命令了
php artisan migrate
您可以阅读有关 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();
});
}
}
$table->string('first_name')->default('DEFAULT');
如果默认值应该为 null,请将其设为可为 null。
$table->string('name')->nullable();
$table->string('name')->nullable()->default('NULL');
$table->string('name')->nullable()->default(NULL);
$table->string('name')->nullable()->default();