使用"artisan migrate"时如何更改时间戳列名?

Jon*_*han 9 php laravel laravel-4

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

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

命令.

我已经做了以下更改.当我使用eloquent查询构建器时,它可以正确生成列名,但是当我使用上面的命令时,它仍然会生成"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';
Run Code Online (Sandbox Code Playgroud)
/* 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();
}
Run Code Online (Sandbox Code Playgroud)

PS我知道修改"核心"不是一个好主意.如果有人能告诉我扩展这些课程的最佳方法,我会非常感激.

rmo*_*bis 19

不要编辑该vendor文件夹下的代码.首先,它通常(默认情况下)不随您的存储库一起提供,因此如果您或其他任何人想要在另一台计算机上工作,您将丢失更改.其次,你做的时候会被覆盖composer update.


好吧,话虽这么说,让我们开始处理这个"修改核心"的恐怖.对于Illuminate\Database\Eloquent\Model.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';

}
Run Code Online (Sandbox Code Playgroud)

那么,对于这个Illuminate\Database\Schema\Blueprint案子......好吧,它变得血腥:

  1. 扩展..\Schema\Blueprint,覆盖您提到的方法.
  2. 扩展..\Schema\Builder,覆盖createBlueprint方法以使用您的新Blueprint类.
    • 还扩展..\Schema\MySqlBuilder到新Builder类的扩展.
  3. 扩展..\Connection,覆盖 getSchemaBuilder方法以使用您的新Builder类.
    • 还扩展..\MySqlConnection,..\PostgresConnection,..\SqlServerConnection..\SQLiteConnection从您的新扩展Connection的类.
    • 注意: ..\MySqlConnection还需要getSchemaBuilder扩展其方法以使用新MySqlBuilder类.
  4. 扩展..\ConnectionFactory,覆盖createConnection方法以使用扩展Connection类.
  5. 创建一个ServiceProvider将新ConnectionFactory类注册为新db.factory组件,并将其添加到您的app/config/app.php文件下providers.

因此,经过半个小时的挖掘,通过Laravel的源代码来解决这个问题,我得出的结论是,在迁移中简单地执行以下操作可能会更容易:

$table->timestamp(BaseModel::CREATED_AT);
$table->timestamp(BaseModel::UPDATED_AT);
Run Code Online (Sandbox Code Playgroud)