当前位置:首页 > IT技术 > Web编程 > 正文

laravel migration 修改字段 php artisan make:migration ...
2022-05-11 10:52:56


php artisan make:migration alter_sources_description --table=life_car_sources
php artisan migrate
php artisan migrate:rollback --step=1 # 回滚变更
<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

/**
* php artisan make:migration alter_sources_description --table=要改的表名称
* php artisan migrate
* php artisan migrate:rollback --step=1 # 回滚变更
*/
class AlterSourcesDescription extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('要改的表名称', function (Blueprint $table) {
DB::statement("ALTER TABLE 要改的表名称 MODIFY vehicle_description varchar(1000) default '' NOT NULL COMMENT '车辆描述';");
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('要改的表名称', function (Blueprint $table) {
DB::statement("ALTER TABLE 要改的表名称 MODIFY vehicle_description varchar(255) default '' NOT NULL COMMENT '车辆描述';");
});
}
}

​Database: Migrations​

新增字段

php artisan make:migration add_字段名_to_表名 --table=表名 

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class AddPayMethodToOrder extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('表名', function (Blueprint $table) {
$table->tinyInteger("字段名")->default(2)->comment("1-xxx 2-yyyy")->after("前面一个字段名");
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('表名', function (Blueprint $table) {
$table->removeColumn("字段名");
});
}
}


本文摘自 :https://blog.51cto.com/u

开通会员,享受整站包年服务立即开通 >