Laravel在Model中编写时创建动态属性

I wonder if it is possible to create or set an attribute when writing in the Model. I tried to do it with a setMutator, but when the attribute is not provided in the list with arguments for a save the value is not created

public function setExampleAttribute($value)
{
    $this->attributes['example'] = 100;
}

So when I do this:

Version::create(['name' => 'mark', 'example' => '50');

The record is saved with 100.

But when I do this:

Version::create(['name' => 'mark');

The record is empty.

Is there a way to do something in between? So before the object is written, add some dynamic content?

It doesn't work, because create() uses fill() to set attribute values. You can try to use $attributes to set default value:

protected $attributes = ['example' => 100];

If you want to add a dynamic attribute, you can use Eloquent creating event inside of which set attribute:

$this->attributes['example'] = $value;

Or call mutator:

$this->setExampleAttribute(null);