Laravel / Lumen将时间戳转换为int

时间:2018-09-15 10:35:55

标签: php laravel casting lumen

使用rate=0创建时间戳记 获取Unix时间戳值。我在模型中将其投射为:

item

但是,purchased_items将unix时间戳记作为字符串而不是int / number。

$table->timestamp('createdAt');

应该是:

protected $casts = [
    'createdAt' => 'datetime:U',
]

我正在使用toArray方法来避免迭代行。因此,$model->toArray();不是一个选择。

1 个答案:

答案 0 :(得分:1)

尝试使用int铸造

(int)$timestamp

您的情况应该是

$casts['createdAt'] = (int)$casts['createdAt'];

请阅读以获取更多信息:http://php.net/manual/en/language.types.type-juggling.php