Laravel5.8-添加、更新、删除数据库字段

发布时间 2023-05-17 10:28:32作者: 德玛东亚

在设计表的时候,往往不能一次性到位,很多的时候会根据需求重新补充缺少的字段  

 

一、添加字段

1 php artisan make:migration add_testmore_add_siteid --table=testmore
2 php artisan make:migration #固定格式 3 add_testmore_add_siteid #对应的生成文件名称,在其中加入对应的字段就行,一次性可以补充一个或者多个字段 4 --table=testmore #指定表名

Schema::table('testmore', function (Blueprint $table) {
  $table->string('siteid', 40)->nullable()->comment('站点ID')->after('age');
});
 

 

二、更新字段类型

 1 php artisan make:migration update_testmore_update_desc --table=testmore
 2 
 3 public function up()
 4 {
 5     Schema::table('testmore', function (Blueprint $table) {
 6         Schema::table('testmore', function (Blueprint $table) {
 7             $table->text('desc', 40)->nullable()->comment('描述')->change();
 8         });
 9     });
10 }

 

三、删除字段

 1 php artisan make:migration del_testmore_del_type --table=testmore
 2 
 3 php artisan make:migration #固定格式
 4 del_testmore_del_type        #文件名称 
 5 --table=testmore                  #表名
 6 
 7 public function up()
 8     {
 9         Schema::table('testmore', function (Blueprint $table) {
10             $table->dropColumn(['type']);
11         });
12     }

 

四、上面的操作也可以在同个文件中进行,写不同的方法即可;