dshakcsq64956 2017-07-11 21:47
浏览 204
已采纳

如何在Laravel中更改自动生成的迁移页面?

When I genarate a migration in Laravel, it automatically looks like this:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}

But I want to work more convenient, I want to let the database handle when I create and update a row instead of doing it myself everytime. I found a way to make this possible like this:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
    });
}

But now everytime I make a migration, I manually have to change this and I don't think that's very convenient. Does anybody know how I can change the automatically generated migration in Laravel?

  • 写回答

2条回答 默认 最新

  • duanqiangwu9332 2017-07-12 06:02
    关注

    If you dig around in the source code you will find that:

    1. There's a file called create.stub in the framework.
    2. This file is used by the MigrationCreator to make migrations.

    In principle you can do the following:

    1. Grab the built-in migration file and move it in another folder in your project (e.g. resouces/stubs probably) Note that you should copy the other stubs in that folder too even if you won't modify them.

    2. Then, override the default migration creator to use this file instead, this should work:

      class MyMigrationCreator extends MigrationCreator {
            protected function stubPath() {
                   return base_path("resources"); //Or something valid         
            }
      }
      

    Then in your application service provider you can do:

    $this->app->instance(MigrationCreator::class, resolve(MyMigrationCreator::class));
    

    This will (hopefully) "trick" laravel into using your migration creator than the default one. However, creating tables is not something that happens so often to justify all this trouble.

    Update: It should extend the migration creator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.