Manually saving data
FieldFoo::make('Foo')->custom()
If you want to be in control of how a field is saved add the custom()
method to the field
Input::make('My custom field', 'foo')
->custom()
->rule('required'),
FieldFoo::make('Foo')->relation()
The relation()
method does the same as custom()
, it is just there for code readability
Select::make('Parent', 'parent_id')
->relation()
->rule('required'),
saveFoo() - save the data
If you used the custom()
or relation()
method on your field, it will be excluded from the default save method, so you must save them yourself via the saveFoo()
hook.
The method is executed AFTER field validation and AFTER the model is created or updated, meaning that you have access to $this->model
even if it is a create
form.
protected function saveFoo($validated_field_value)
{
//the validate field value is passed to the hook
$field_value = $validated_field_value;
//or get the field value via the fields name property
$field_value = $this->foo;
//or via form_data
$field_value = data_get($form_data, 'foo');
//save the data
$this->model->foo = $field_value;
}
You do NOT have to prefix with FormData
Example method name for an array field
form_data.people.name
will becomesavePeopleName($validated_value)
Example method name for
form_data.some_field_name
will becomesaveSomeFieldName($validated_value)
Example; form_data.price
public function savePrice($validated_value)
{
$this->model->price = $validated_value;
$this->model->save();
}
Last updated
Was this helpful?