Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中更改自动生成的迁移页面?_Php_Laravel_Laravel 5_Migration - Fatal编程技术网

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

Php 如何在Laravel中更改自动生成的迁移页面?,php,laravel,laravel-5,migration,Php,Laravel,Laravel 5,Migration,在Laravel中生成迁移时,它会自动显示如下: public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } public function up() { Schema::create('comments', functi

在Laravel中生成迁移时,它会自动显示如下:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}
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'));
    });
}
但我希望工作更方便,我希望在创建和更新行时让数据库处理,而不是每次自己处理。 我找到了一种方法,可以这样做:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}
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'));
    });
}

但现在每次我进行迁移时,我都必须手动更改,我认为这不是很方便。有人知道我如何在Laravel中更改自动生成的迁移吗?

如果您仔细研究源代码,您会发现:

  • 框架中有一个名为的文件
  • 此文件由用于进行迁移
  • 原则上,您可以执行以下操作:

  • 抓取内置迁移文件并将其移动到项目中的另一个文件夹中(例如,可能是Resources/stubs)。请注意,即使不修改其他stubs,也应复制该文件夹中的其他stubs

  • 然后,覆盖默认迁移创建者以使用此文件,这应该可以工作:

    class MyMigrationCreator extends MigrationCreator {
          protected function stubPath() {
                 return base_path("resources"); //Or something valid         
          }
    }
    
  • 然后,在应用程序服务提供商中,您可以执行以下操作:

    $this->app->instance(MigrationCreator::class, resolve(MyMigrationCreator::class));
    
    这将(希望)诱使laravel使用您的迁移创建者,而不是默认的迁移创建者。然而,创建表并不是为了证明所有这些麻烦的合理性而经常发生的事情


    更新:它应该扩展迁移创建者。

    在Laravel 5.6中,似乎不可能重写MigrationCreator类,因为它直接在MigrationServiceProvider中使用:

    protected function registerCreator() {
        $this->app->singleton('migration.creator', function ($app) {
            return new MigrationCreator($app['files']);
        });
    }
    
    但您可以用下一种方法破解MigrationServiceProvider:

  • 在项目根目录中创建目录:
    overrides/illumb/Database
  • 将文件
    vendor/laravel/framework/src/illumb/Database/MigrationServiceProvider.php
    复制到
    overrides/illumb/Database
    文件夹中(请注意,类名称空间将保持不变)
  • 修改
    覆盖/illumb/Database/MigrationServiceProvider.php

    protected function registerCreator() {
        $this->app->singleton('migration.creator', function ($app) {
             return new MyMigrationCreator($app['files']);
        });
    }
    
  • 修改您的
    composer.json
    “照亮\\”
    ):

  • 运行
    composer dump autoload
    。作曲家会说

    `Warning: Ambiguous class resolution, "Illuminate\Database\MigrationServiceProvider" was found in both "$baseDir . '/overrides/Illuminate/Database/MigrationServiceProvider.php" and "/vendor/laravel/framework/src/Illuminate\Database\MigrationServiceProvider.php", the first will be used.`
    
  • 现在,将使用假的
    MigrationServiceProvider
    代替Laravel,但您将无法使用原始的。这就是我们复制整个文件的原因


    实际上,通过这种方式,您可以覆盖
    MigrationCreator
    类,但其中的代码量很大,您实际上需要使用
    MyMigrationCreator
    类对其进行扩展,并覆盖一些方法,如
    stubPath()
    create()
    。但是,
    MigrationServiceProvider
    可以被非常安全地覆盖,因为它包含几个小方法,除非您对模型做了一些奇怪的事情,否则在Laravel 6

    Elounce默认情况下管理这些字段之前,这些小方法不太可能被更改。你说你每次都必须自己做是什么意思?@Bytewave我希望能够在默认情况下在迁移中创建和更新这两行内容,但默认值目前是$time->timestamps(),这在Laravel 5.6上不起作用,可能在几个早期的小版本中也不起作用。MigrationServiceProvider延迟并直接创建MigrationCreator类实例的原因:$this->app->singleton('migration.creator',function($app){return new MigrationCreator($app['files']);};最糟糕的是,即使在boot()中重新声明$this->app->singleton('migration.creator',function($app){}),它也会被覆盖,因为MigrationServiceProvider被延迟。我还在寻找解决办法=(@Swayok尝试使用
    $this->app->instance
    而不是
    singleton
    ,然后自己预解析。希望在框架中将其标记为已解析。然后,每次调用服务提供商时,它都会被重新加载,而这不是一种工作方式,因为您不需要每次加载应用程序时都使用此类至少当您在CLI中运行它时,应该限制它,因为此时您需要迁移创建者。我知道这不是一个好的解决方案,但似乎没有正式的方法来更改存根。