在 laravel 中获取 Unix 时间戳



我将created_at和updated_at存储为 unix 时间戳

为此,我将以下行添加到我的模态中,它工作正常。

class Test extends Model
{
protected $dateFormat = 'U';
protected $fillable = [
'user_id','question_id','answer',
];
}

但问题是当我检索记录时,它给了我可读的格式而不是 UNIX 时间戳,所以我如何获得存储在数据库中的值,即 (1593624368(。 在数据库中,我使用 int(11( 数据类型进行created_at和updated_at

created_at: 2020-07-01T17:45:03.000000Z,
updated_at: 2020-07-02T08:53:14.000000Z,

你可以使用Laravel日期铸造

对于您的情况:

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
];

最新更新