Php 如何在Laravel中更改自动生成的迁移页面?
在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
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中更改自动生成的迁移吗?如果您仔细研究源代码,您会发现:
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 6Elounce默认情况下管理这些字段之前,这些小方法不太可能被更改。你说你每次都必须自己做是什么意思?@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中运行它时,应该限制它,因为此时您需要迁移创建者。我知道这不是一个好的解决方案,但似乎没有正式的方法来更改存根。