سؤال

I'm trying to strip non-numeric characters on an attribute before saving, but the new value is not saved.

My model looks like this:

class Model extends \Eloquent {
    public function setColumnAttribute($value) {
        $value = (int)preg_replace('/[^0-9]/', '', $value);

        return $value;
    }
}

That method do run, if I add a dd($value) in it, that dumps.

I update the model with this:

Model::find($id)->update($attributes);

Why doesn't it save the value given from the setColumnAttribute method? What am I missing?

هل كانت مفيدة؟

المحلول

By searching on eloquent mutator I find out you had to insert it in the $this->attributes array instead of returning the value.

class Model extends \Eloquent {
    public function setColumnAttribute($value) {
        $value = (int)preg_replace('/[^0-9]/', '', $value);

        $this->attributes['column'] = $value;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top