注意: 这种方法只适合新增字段, 修改和删除是不行的.
Schema::create 改为 Schema::table , 然后把其他已经存在的字段注释掉, 如下图所示, 只需要在原来基础上添加这些你想要添加的字段:
- <?PHP
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreatePostsTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- // Schema::create('posts', function (Blueprint $table) {
- // $table->increments('id');
- // $table->string('title');
- // $table->text('body');
- // $table->unsignedBigInteger('auithor_id');
- // $table->foreign('author_id')->references('id')->on('users');
- // $table->timestamp('published_at')->nullable();
- // $table->timestamps();
- // });
- Schema::table('posts', function (Blueprint $table) {
- // $table->increments('id');
- $table->string('title');
- $table->text('body');
- $table->unsignedBigInteger('author_id');
- $table->foreign('author_id')->references('id')->on('users');
- $table->timestamp('published_at')->nullable();
- // $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('posts');
- }
- }
改完之后, 把 migrationsb 表里面的这个 migrate 记录删除掉, 不然 Laravel 会发现已经 migrate 过, 就不会更改.
如果没存在其他表, 一开始执行 PHP artisan migrate 即可, 貌似这句会全部执行一遍, 会报错其他表已经存在;
所以, 我需要执行这个指定的表;
首先执行 PHP artisan tinker; 然后 (new CreatePostsTable)->up() 执行这个表的 up 方法即可, 刷新数据表就可以看到新增的字段啦!
来源: http://www.bubuko.com/infodetail-3505624.html