This is an example code which is loaded through a mass job assignment:
someModel::create([
'test' => $data->test
]);
In "someModel" i have following mutator:
public function setTestAttribute($value)
{
$this->attributes['test'] = '1234'; //Hash::make($value) instead of 1234 is not working either
}
When I run the code, contents of the "test" column in db is not 1234, it's the data that comes from the $data variable. So the mutator is not manipulating the data. Am I missing something basic here?
I have tried to do a dd('test') inside of the setTestAttribute, and it's showing up perfectly, so it is being initialised, it's just not setting/overriding the value.
"Test" column in db is of type: varchar, and test is fillable in the model settings.
Laravel model events and mutators are triggered only with ->save() .
->update([...]) and ->create([...]) is for generating SQL "CREATE" and "UPDATE" operations, and they doesn't consider eloquent features.
So (even I also like to use "create" and "update" but for working with mutators) try to use something like this:
$someModelInstance = new someModel();
$someModelInstance->test = $data->test;
$someModelInstance->save();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.