Laravel: 更改数据库中时间戳的名称
Laravel: Change the timestamps' names in the database
我正在尝试重命名数据库中 user
table 的时间戳列(created_at
和 updated_at
)。我已经看到 this answer 但是当我像这样重写 CREATED_AT
和 UPDATED_AD
常量时:
class User extends Authenticatable
{
const CREATED_AT = 'user_creation_date';
const UPDATED_AT = 'user_update_date';
...
}
它所做的只是重命名 User
模型的属性,即 $user->user_creation_date
和 $user->user_update_date
。数据库列保持不变。我应该如何在保持自动更新功能的同时重命名数据库的列?
感谢您的帮助。
您可以使用获取属性,例如
class User extends Authenticatable
{
protected $timestamps = true;
protected $hidden = ['created_at', 'updated_at'];
protected $appends = ['user_creation_date', 'user_update_date'];
public function getUserCreationDateAttribute(){
return $this->created_at;
}
public function getUserUpdateDateAttribute(){
return $this->updated_at;
}
}
现在您将在 user_creation_date
和 user_update_date
字段中获得 created_at
和 updated_at
列数据。当您 return array
或 json
响应或将 object
转换为 array
或 json
。
您需要在 database/migrations
中更新您的用户 table 迁移文件,它将是一个类似于 2014_10_12_000000_create_users_table.php
.
的文件
您可能有 $table->timestamps();
在 Schema::create
通话中。
查看 vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php
中 timestamp()
的代码显示:
public function timestamps($precision = 0)
{
$this->timestamp('created_at', $precision)->nullable();
$this->timestamp('updated_at', $precision)->nullable();
}
所以:
Schema::create('users', function (Blueprint $table) {
// ..
$table->timestamps();
});
删除对 $table->timestamps();
的调用并添加要调用时间戳的两列:
Schema::create('users', function (Blueprint $table) {
// ..
$this->timestamp('user_creation_date', 0)->nullable();
$this->timestamp('user_update_date', 0)->nullable();
});
您将需要再次 运行 迁移,请务必备份数据,因为这会丢失 table 并重新创建它们。
希望对您有所帮助。
我正在尝试重命名数据库中 user
table 的时间戳列(created_at
和 updated_at
)。我已经看到 this answer 但是当我像这样重写 CREATED_AT
和 UPDATED_AD
常量时:
class User extends Authenticatable
{
const CREATED_AT = 'user_creation_date';
const UPDATED_AT = 'user_update_date';
...
}
它所做的只是重命名 User
模型的属性,即 $user->user_creation_date
和 $user->user_update_date
。数据库列保持不变。我应该如何在保持自动更新功能的同时重命名数据库的列?
感谢您的帮助。
您可以使用获取属性,例如
class User extends Authenticatable
{
protected $timestamps = true;
protected $hidden = ['created_at', 'updated_at'];
protected $appends = ['user_creation_date', 'user_update_date'];
public function getUserCreationDateAttribute(){
return $this->created_at;
}
public function getUserUpdateDateAttribute(){
return $this->updated_at;
}
}
现在您将在 user_creation_date
和 user_update_date
字段中获得 created_at
和 updated_at
列数据。当您 return array
或 json
响应或将 object
转换为 array
或 json
。
您需要在 database/migrations
中更新您的用户 table 迁移文件,它将是一个类似于 2014_10_12_000000_create_users_table.php
.
您可能有 $table->timestamps();
在 Schema::create
通话中。
查看 vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php
中 timestamp()
的代码显示:
public function timestamps($precision = 0) { $this->timestamp('created_at', $precision)->nullable(); $this->timestamp('updated_at', $precision)->nullable(); }
所以:
Schema::create('users', function (Blueprint $table) {
// ..
$table->timestamps();
});
删除对 $table->timestamps();
的调用并添加要调用时间戳的两列:
Schema::create('users', function (Blueprint $table) {
// ..
$this->timestamp('user_creation_date', 0)->nullable();
$this->timestamp('user_update_date', 0)->nullable();
});
您将需要再次 运行 迁移,请务必备份数据,因为这会丢失 table 并重新创建它们。
希望对您有所帮助。