这里的答案将扩展 @caddy dz 的答案,他恰好和我坐在一起。
所有需要知道的事情
取消自动管理时间戳
public $timestamps = false; // <-- deactivate the automatic handling
更改表属性名称
const CREATED_AT = 'creation_date'; // <--- change the names
const UPDATED_AT = 'last_update';
源文档:
https://laravel.com/docs/5.8/eloquent#eloquent-model-conventions
默认情况下,Eloquent 期望 created_at 和 updated_at 列
存在于您的桌子上。如果您不希望有这些列
由 Eloquent 自动管理,将 $timestamps 属性设置为
你的模型是假的:
创建访问器
class User extends Model
{
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
public function getFirstNameAttribute($value)
{
// do whatever you want here (change and mutate the value)
return ucfirst($value);
}
}
首先要知道,访问器是一个全局概念
雄辩,可以写所有属性,而不仅仅是
getCreatedAtAttribute 或 getUpdatedAtAttribute。
要知道的第二件事是,无论列的名称是什么,即
在 camle case (firstName) 或 with _ (first_name) 雄辩的知道
与之相匹配。访问器的格式应该是
get[NameOfATtribute]Attribute pascal case(camle case 但首先
字母也是大写的)。
三个方法参数保存列的值
题。下面是一个显示其使用方式的 sn-p
$user = App\User::find(1);
$firstName = $user->first_name; //|=> first_name => getFirstNameAttribute(columnVal)
分辨率清晰。
first_name(列名)=> getFirstNameAttribute(列值)
所有的 sn-ps 都来自文档:https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators
让我们应用所有这些
首先,我们不需要在迁移中使用$table->timestamps(),因此我们对以下内容进行了更改。
Schema::create('cars', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamp('cardata_time', 0)->nullable();
$table->timestamp('car_time', 0)->nullable();
});
然后我们将修改应用于我们的模型:
- 我们停用了时间戳的自动处理。
- 覆盖时间戳列名称。
- 并创建访问器。
这取决于我们想要什么。如果我们只想在此处执行上述操作,则 sn-p 表明:
// deactivate auto timestamps management
public $timestamps = false;
// change the columns names
const CREATED_AT = 'car_time';
const UPDATED_AT = 'cardata_time';
// creating the accessors (respect the naming)
protected function getCarTimeAttribute($value) //car_time => CarTime
{
// <-- do whatever you want here (example bellow)
$format = "Y-m-d\TH:i:s\Z";
$datetime = new DateTime($value);
return $datetime->format($format);
}
protected function getCardataTimeAttribute($value) //cardata_time => CardataTime
{
// <-- do whatever you want here
$format = "Y-m-d\TH:i:s\Z";
$datetime = new DateTime($value);
return $datetime->format($format);
}
完全重命名属性
如果您想要使用另一个访问名称。然后我的朋友@caddy dz 所做的就是要走的路。碰巧和我坐在一起。并鼓励我扩展答案。 (hhhh)
你需要知道
$appends 和 $hidden
序列化 API 的一部分。
https://laravel.com/docs/master/eloquent-serialization#appending-values-to-json
https://laravel.com/docs/master/eloquent-serialization#hiding-attributes-from-json
$appends 允许我们向模型添加属性。这在桌子上不存在。我们还需要为它们创建一个访问器。
class User extends Model
{
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['is_admin'];
// ........
/**
* Get the administrator flag for the user.
*
* @return bool
*/
public function getIsAdminAttribute()
{
return $this->attributes['admin'] == 'yes';
}
}
和
$hidden 允许我们从模型中删除和限制属性。就像密码字段一样。
文档示例:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password'];
}
因此,我们需要做的是隐藏持有时间的属性,这些属性想要更改为其他内容。
// remove the old attributes names
protected $hidden = ['car_time', 'cardata_time']; // renaming those
// append the new one \/ \/ <- to those
protected $appends = ['car_crated_at', 'cardata_created_at']; // names just for illustration
protected function getCarCreatedAtAttribute($value) // car_created_at => CarCreatedAt
{
$format = "Y-m-d\TH:i:s\Z";
$datetime = new DateTime($value);
return $datetime->format($format);
}
protected function getCardataCreatedAtAttribute($value) // cardata_created_at => CardataCreatedAt
{
$format = "Y-m-d\TH:i:s\Z";
$datetime = new DateTime($value);
return $datetime->format($format);
}
将其应用于不同的模型
基本思想是创建一个基本模型,然后在创建模型时对其进行扩展。
无一例外地格式化模型的所有时间属性
如果您想要为模型中的所有时间属性应用格式。
然后覆盖serializeDate() 方法。在实践中写一个特征,然后你就可以应用它。否则为基础模型。
下面的答案很好地涵盖了它:
https://stackoverflow.com/a/41569026/7668448
从历史上看,这个线程很有趣:
https://github.com/laravel/framework/issues/21703
在碳级序列化
在 laravel 5.7 及更高版本的文档中(我检查了 [仅文档]):
https://laravel.com/docs/master/eloquent-serialization#date-serialization
我们可以在碳序列化级别更改格式。但碰巧有一个过去的错误。通常是固定的,但我没有尝试。如果我没记错的话,错误在 5.7 中并在 5.7 中修复。上面的 git 链接讨论一下。
片段:
class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Carbon::serializeUsing(function ($carbon) {
return $carbon->format('U');
});
}
___THE_END ^ ^