laraval migration 新增字段或者修改字段的方法

时间:2023-03-09 09:54:38
laraval migration 新增字段或者修改字段的方法

1.进入项目根目录执行artisan命令生成migration文件,可以指定--table和--path参数,会在对应目录下生成migration文件。

php artisan make:migration alter_table_his_decisions  --table=his_decisions --path=database/migrations/ca/

2.在migration文件中的up方法中新增或者修改字段

    /**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('his_decisions', function (Blueprint $table) {
$table->string('primary_refuse_reason')->default('')->comment('拒绝主原因');
$table->string('secondary_refuse_reason')->default('')->comment('拒绝子原因');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('his_decisions', function (Blueprint $table) {
$table->drop_column('primary_refuse_reason');
$table->drop_column('secondary_refuse_reason');
});
}

3.执行artisan命令来使变更生效,可以指定path参数到目录级别

php artisan migrate --path=database/migrations/ca/ 

ps:

一个很好用的参数来查看artisan命令可以跟哪些参数,比如想知道迁移命令后接的参数

php artisan migrate --help