laravel

注意:这种方法只适合新增字段,修改和删除是不行的。


Schema::create改为 Schema::table ,然后把其他已经存在的字段注释掉,如下图所示,只需要在原来基础上添加这些你想要添加的字段:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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方法即可,刷新数据表就可以看到新增的字段啦!

原文地址:https://www.cnblogs.com/pyspang/p/12693643.html