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

  • 相关阅读:
    动态内存
    用c的数组简单的模拟了入栈
    c++实验,需要的人都知道是啥
    c语言的一个简单的链表
    c++的引用
    c++的一个有趣的程序
    奥运五环的绘制
    网页中的事件与事件响应
    响应事件的示例
    关于window.onload,window.onbeforeload与window.onunload
  • 原文地址:https://www.cnblogs.com/pyspang/p/12693643.html
Copyright © 2020-2023  润新知