Laravel Eloquent 脏检查碳日期

Rob*_*Rob 3 php laravel laravel-5

我的模型中有一个published_at字段设置为碳日期。

class Model {

    protected $dates = ['published_at'];

    ....

    public function setPublishedAtAttribute($val)
    {
         $this->attributes['published_at'] = \Carbon\Carbon::createFromTimeStamp(strtotime($val));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是可由用户设置的字段。当我dirty对其进行检查时:

$article->fill($data);
echo $article->isDirty() ? 'true' : 'false';
Run Code Online (Sandbox Code Playgroud)

它总是会出现dirty。我做错了什么还是因为它试图比较两个 Carbon 对象?

Joh*_* N. 5

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;
}
Run Code Online (Sandbox Code Playgroud)