laravel 使用“工匠迁移”时如何更改时间戳列名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17735549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:09:22  来源:igfitidea点击:

How can I change timestamps column name when using "artisan migrate"?

phplaravellaravel-4

提问by Jonathan

I am struggling to change the timestamp column names which are generated by

我正在努力更改生成的时间戳列名称

php artisan migrate

command.

命令。

I have already made following change. When I use eloquent query builder, it can correctly generate the column name, but when I using the command above, it still generates "created_at", "updated_at" and "deleted_at". Can anyone help me out? Thanks a lot.

我已经进行了以下更改。当我使用eloquent query builder时,它可以正确生成列名,但是当我使用上面的命令时,它仍然生成“created_at”、“updated_at”和“deleted_at”。谁能帮我吗?非常感谢。

/* vendor\framework\src\Illuminate\Database\Eloquent\Model.php */

/**
 * The name of the "created at" column.
 *
 * @var string
 */
const CREATED_AT = 'datetime_created';

/**
 * The name of the "updated at" column.
 *
 * @var string
 */
const UPDATED_AT = 'datetime_updated';

/**
 * The name of the "deleted at" column.
 *
 * @var string
 */
const DELETED_AT = 'datetime_deleted';


/* vendor\framework\src\Illuminate\Database\Schema\Blueprint.php */

/**
 * Indicate that the timestamp columns should be dropped.
 *
 * @return void
 */
public function dropTimestamps()
{
    $this->dropColumn('datetime_created', 'datetime_updated');
}

/**
 * Add a "deleted at" timestamp for the table.
 *
 * @return void
 */
public function softDeletes()
{
    $this->timestamp('datetime_deleted')->nullable();
}
/**
 * Add creation and update timestamps to the table.
 *
 * @return void
 */
public function timestamps()
{
    $this->timestamp('datetime_created');

    $this->timestamp('datetime_updated');
}
/**
 * Add a "deleted at" timestamp for the table.
 *
 * @return void
 */
public function softDeletes()
{
    $this->timestamp('datetime_deleted')->nullable();
}

P.S. I know it's not a good idea to modify the "core". If someone can tell me the best way to extend those classes I would really appreciate it.

PS我知道修改“核心”不是一个好主意。如果有人能告诉我扩展这些类的最佳方式,我将不胜感激。

回答by rmobis

Don't ever edit the code under the vendorfolder. First, it's usually (by default) not carried with your repository, so you'd lose the changes if you or anyone else ever wanted to work on another machine. Second, it'd be overwritten at the moment you do a composer update.

永远不要编辑vendor文件夹下的代码。首先,它通常(默认情况下)不与您的存储库一起携带,因此如果您或其他任何人想要在另一台机器上工作,您将丢失更改。其次,它会在您执行composer update.



Well, that being said, let's start dealing with this "modifying the core" horror. For the Illuminate\Database\Eloquent\Model.php, simply create a base model, from which you'll be extending all your subsequent models, and overwrite the constants in it:

好吧,话虽如此,让我们开始处理这种“修改核心”的恐怖。对于Illuminate\Database\Eloquent\Model.php,只需创建一个基本模型,您将从中扩展所有后续模型,并覆盖其中的常量:

app/models/BaseModel.php

应用程序/模型/BaseModel.php

abstract class BaseModel extends Eloquent {

    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'datetime_created';

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'datetime_updated';

    /**
     * The name of the "deleted at" column.
     *
     * @var string
     */
    const DELETED_AT = 'datetime_deleted';

}

Then, for the Illuminate\Database\Schema\Blueprintcase... Well, it gets bloody:

然后,对于这种Illuminate\Database\Schema\Blueprint情况......好吧,它变得血腥:

  1. Extend ..\Schema\Blueprint, overwriting the methods you mentioned.
  2. Extend ..\Schema\Builder, overwriting createBlueprintmethod to use your new Blueprintclass.
    • Also extend ..\Schema\MySqlBuilderto extend from your new Builderclass.
  3. Extend ..\Connection, overwriting getSchemaBuildermethod to use your new Builderclass.
    • Also extend ..\MySqlConnection, ..\PostgresConnection, ..\SqlServerConnectionand ..\SQLiteConnectionto extend from your new Connectionclass.
    • Note:..\MySqlConnectionalso needs to have its getSchemaBuildermethod extended to use your new MySqlBuilderclass.
  4. Extend ..\ConnectionFactory, overwriting createConnectionmethod to use your extended Connectionclasses.
  5. Create a ServiceProviderto register your new ConnectionFactoryclass as the new db.factorycomponent, and add it on your app/config/app.phpfile, under providers.
  1. Extend ..\Schema\Blueprint,覆盖您提到的方法。
  2. Extend ..\Schema\Builder,覆盖createBlueprint方法以使用您的新Blueprint类。
    • 还可以..\Schema\MySqlBuilder从您的新Builder课程中扩展。
  3. Extend ..\Connection,覆盖 getSchemaBuilder方法以使用您的新Builder类。
    • 还扩展..\MySqlConnection..\PostgresConnection..\SqlServerConnection..\SQLiteConnection从您的新扩展Connection的类。
    • 注意:..\MySqlConnection还需要getSchemaBuilder扩展其方法以使用您的新MySqlBuilder类。
  4. Extend ..\ConnectionFactory,覆盖createConnection方法以使用您的扩展Connection类。
  5. 创建一个ServiceProvider以将您的新ConnectionFactory类注册为新db.factory组件,并将其添加到您的app/config/app.php文件中,在providers.

So, after half an hour digging through Laravel's source code to figure that out, I came to the conclusion that it would probablybe easier to simply do the following on your migrations:

因此,在通过 Laravel 的源代码挖掘半小时以弄清楚这一点后,我得出的结论是,在迁移中简单地执行以下操作可能会更容易:

$table->timestamp(BaseModel::CREATED_AT);
$table->timestamp(BaseModel::UPDATED_AT);