在laravel中表创建成功后
如何在表中新增字段
运行命令
$ php artisan make:migration add_field_into_testtable
然后打开文件database/migrations/2022_09_02_1238_add_field_into_testtable.php
内容如下
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddFieldIntoTesttable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('table_testtable',function (Blueprint $table) {
$table->string('field')->after('name')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('table_testtable', function (Blueprint $table) {
$table->dropColumn('field');
});
}
}
执行命令
$ php artisan migrate
Migrating: 2022_09_02_1238_add_field_into_testtable
Migrated: 2022_09_02_1238_add_field_into_testtable (0.06 seconds)
Administrator@DESKTOP-64TSROO MINGW64 /d/phpstudy_pro/WWW (feature-yansiyu)
回滚操作
$ php artisan migrate:rollback
Rolling back: 2022_09_02_1238_add_field_into_testtable
Rolled back: 2022_09_02_1238_add_field_into_testtable (0.06 seconds)
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
转载请注明:laravel中migrate运行迁移文件给数据库表新增字段 出自老鄢博客 | 欢迎分享