I have a model Foo that corresponds to a table with the following columns.
id
description
user_id
I\'m setting the attributes of the Foo model individual
I know it is too late, but you can register the saving
operation by override the boot
function in your model:
protected static function boot() {
parent::boot();
static::saving(function($model) {
$savable = [...];
if (count($savable) > 0) {
$model->attributes = array_intersect_key($model->attributes, array_flip($savable));
}
});
}
This is untested code, but the idea is to remove the attributes that have not intersect with the variable savable
before saving the model. The variable savable
is an array of the savable attributes. For example $savable = ['foo', 'bar']
will save only the foo
and bar
attributes.
Pros: You can mass assign any attributes that you want without harm to fillable
or guarded
.
Cons: The attributes that are not marked as savable
, will be deleted from the model after saving.