我的模型中有一个published_at字段,我将其设置为碳排放日期。
class Model {
protected $dates = ['published_at'];
....
public function setPublishedAtAttribute($val)
{
$this->attributes['published_at'] = \Carbon\Carbon::createFromTimeStamp(strtotime($val));
}
}这是一个可由用户设置的字段。当我对其运行dirty检查时:
$article->fill($data);
echo $article->isDirty() ? 'true' : 'false';它总是出现在dirty上。我做错了什么吗,或者这是因为它试图比较两个碳物体?
发布于 2017-06-28 23:25:12
现在已使用Laravel 5.5修复此问题。
对于所有使用旧版Laravel的用户:
只需像这样重写您的getDirty()方法:
public function getDirty()
{
$dirty = parent::getDirty();
foreach ($dirty as $key => $value) {
if (!array_key_exists($key, $this->original)) {
continue;
}
// unset any non-changed date values
if ($this->isDateAttribute($key)) {
$old = new Carbon($this->original[$key]);
$new = new Carbon($value);
if ($old->eq($new)) {
unset($dirty[$key]);
}
}
}
return $dirty;
}发布于 2015-06-12 12:29:48
最终实现这一点的最好方法就是以mysql格式正确地格式化日期,然后让Laravel来处理剩下的事情。
因此,在输入日期之前:
$data['published_at'] = '2015-03-03'; // format here;即使在setPublishedAtAttribute方法中对日期字符串进行硬编码,它似乎始终会触发true的脏标志。
https://stackoverflow.com/questions/29432544
复制相似问题