Laravel - Mutator doesn't seem to work on update

谁说胖子不能爱 提交于 2020-06-17 09:39:11

问题


I have the following mutator:

public function setFormattedCriteriaAttribute($value)
{
    $this->attributes['formatted_criteria'] = serialize($value);
}

When I call the following why doesn't it update the formatted_criteria value - note the field is listed in my fillable attributes array ?

$jobAlert = JobAlert::findOrFail($id);

$jobAlert->update([
        'frequency' => $request->frequency,
        'criteria'  => $criteria,
        'formatted_criteria' => ['test']
]);

回答1:


Be sure formated_criteria in your $fillable variable.

Update

if you have casts array in your model modify else add.

protected $casts = [
        'formatted_criteria'    => 'array',
    ];

then update your field as LONGTEXT with binary



来源:https://stackoverflow.com/questions/61564951/laravel-mutator-doesnt-seem-to-work-on-update

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!