gpt4 book ai didi

laravel - 如何在模型本身的 Laravel 中将 unix 时间戳转换为 Carbon 实例?

转载 作者:行者123 更新时间:2023-12-02 14:57:19 25 4
gpt4 key购买 nike

我已经拥有的:

在模型中有这个:

protected $dates = [ 'applied_date' ];

在数据库中 applied_date 以这种格式存储 2018-05-11 13:31:59

所以在 View 中我可以像下面这样格式化它:

$applied_date->format('m/d/Y')

我想要的:

在数据库中 applied_date 已经以这种格式存储 1530205690(unix 时间戳)。

我想在 View 中格式化$applied_date->format('m/d/Y'),我该如何实现?

注意:我们可以做 Carbon::createFromTimestamp($applied_date)->format('m/d/Y');

最佳答案

你可以使用 laravel 属性 https://laravel.com/docs/5.6/eloquent-mutators

像这样

/**
* Get formatted applied_date.
*
* @return string
*/
public function getDateAttribute()
{
$date = Carbon::createFromTimestamp($this->applied_date)->format('m/d/Y');

return $date;
}

然后在你的 View 中你可以这样做{{ $model->date }}

关于laravel - 如何在模型本身的 Laravel 中将 unix 时间戳转换为 Carbon 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com