删除 eloquent 个模型中的日期更改器
Remove date mutator in eloquent models
我有一个模型,其 created_at
值定义为 Unix 时间戳。
我使用 $model = Model::where(...)->first()
检索我的模型实例
当我 var_dump($model->created_at)
我得到一个 Illuminate\Support\Carbon
实例而不是我的整数时间戳。
根据the documentation(强调我的):
By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon, which extends the PHP DateTime class to provide an assortment of helpful methods. You may customize which dates are automatically mutated, and even completely disable this mutation, by overriding the $dates property of your model
我已经尝试将 protected $dates = []
添加到我的模型中,但我仍然得到 Carbon
对象而不是整数。
如果我禁用时间戳 (public $timestamps = false;
) 它可以工作,但是当我创建新条目时我不会插入我的时间戳 - 我需要它。
您尝试过使用:
protected $casts = [
'created_at' => 'integer'
];
我有一个模型,其 created_at
值定义为 Unix 时间戳。
我使用 $model = Model::where(...)->first()
当我 var_dump($model->created_at)
我得到一个 Illuminate\Support\Carbon
实例而不是我的整数时间戳。
根据the documentation(强调我的):
By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon, which extends the PHP DateTime class to provide an assortment of helpful methods. You may customize which dates are automatically mutated, and even completely disable this mutation, by overriding the $dates property of your model
我已经尝试将 protected $dates = []
添加到我的模型中,但我仍然得到 Carbon
对象而不是整数。
如果我禁用时间戳 (public $timestamps = false;
) 它可以工作,但是当我创建新条目时我不会插入我的时间戳 - 我需要它。
您尝试过使用:
protected $casts = [
'created_at' => 'integer'
];