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
How can I change timestamps column name when using "artisan migrate"?
提问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 vendor
folder. 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\Blueprint
case... Well, it gets bloody:
然后,对于这种Illuminate\Database\Schema\Blueprint
情况......好吧,它变得血腥:
- Extend
..\Schema\Blueprint
, overwriting the methods you mentioned. - Extend
..\Schema\Builder
, overwritingcreateBlueprint
method to use your newBlueprint
class.- Also extend
..\Schema\MySqlBuilder
to extend from your newBuilder
class.
- Also extend
- Extend
..\Connection
, overwritinggetSchemaBuilder
method to use your newBuilder
class.- Also extend
..\MySqlConnection
,..\PostgresConnection
,..\SqlServerConnection
and..\SQLiteConnection
to extend from your newConnection
class. - Note:
..\MySqlConnection
also needs to have itsgetSchemaBuilder
method extended to use your newMySqlBuilder
class.
- Also extend
- Extend
..\ConnectionFactory
, overwritingcreateConnection
method to use your extendedConnection
classes. - Create a
ServiceProvider
to register your newConnectionFactory
class as the newdb.factory
component, and add it on yourapp/config/app.php
file, underproviders
.
- Extend
..\Schema\Blueprint
,覆盖您提到的方法。 - Extend
..\Schema\Builder
,覆盖createBlueprint
方法以使用您的新Blueprint
类。- 还可以
..\Schema\MySqlBuilder
从您的新Builder
课程中扩展。
- 还可以
- Extend
..\Connection
,覆盖getSchemaBuilder
方法以使用您的新Builder
类。- 还扩展
..\MySqlConnection
,..\PostgresConnection
,..\SqlServerConnection
并..\SQLiteConnection
从您的新扩展Connection
的类。 - 注意:
..\MySqlConnection
还需要getSchemaBuilder
扩展其方法以使用您的新MySqlBuilder
类。
- 还扩展
- Extend
..\ConnectionFactory
,覆盖createConnection
方法以使用您的扩展Connection
类。 - 创建一个
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);