當前位置:首頁 > IT技術(shù) > 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

開通會員,享受整站包年服務(wù)立即開通 >