Relations
relation()
relation()The form component supports all types of relationships!
Just add ->relation() to any field and handle the data in the saveFoo() hook. Instead of writing default methods to support all Laravel relationships the saveFoo() hook gives you total freedom on how you want to manipulate the data before it is being saved. This way you can define any type of relationship you want.
You are responsible for saving the data
The relationship fields are ignored in the default save method. If you do not handle it with the saveFoo() hook it will be as if it didn't exist in the form at all. The method is executed after the model is saved. Read more on Manually saving data.
Define the field
Checkboxes::make('Attach to Events', 'event_ids')//make sure that the name doesn't collide with a model property
->options($this->events)//if using checkboxes read the documentation about integer values
->relation() //tell the component that this field is a relationship
->rules('nullable|array|exists:events,id'),saveFoo() - save the data
You can omit FormData in the method name, if it is an array like form_data.organization.name the method name becomes saveOrganizationName()
The data is accessed via data_get($relationship_data, 'foo') or the fields name property, like $this->foo, depending on how you setup your form Add the saveFoo() method to your component. The relationship will be passed with the same name as the field.
Example with a field named 'event_ids'
protected function saveEventIds($validated_value)
{
// the field data is accessed via the fields name property
$selected_events = $validated_value;
//or depending on your form setup
$selected_events = $this->event_ids;
// save the data
$this->model->...
}BelongsTo relationship
BelongsTo relationshipYou usually don't have to use the relation() option for a BelongsTo relationship because you can use a simple select field.
Example BelongsTo, no->relation() needed.
Auto populate the relationship field if there is only one value
See the autoSelectSingleArrayValue() docs ->
ManyToMany relationship
In this edit form example a Location can be related to many Events.
Existing relations and options
We will use a fake model attribute, event_ids, that does not exist as a column in the database. We use the beforeFormProperties() method to set existing relations as an appended attribute before form_data is set.
We also need to get available events to use as options for the checkboxes.
Define the field
We will use checkboxes to display available events in the form. We also check if there are any available Events before adding the field to the form. Add the relation() property to the field.
Save the data
Add the saveFoo() to the component.
HasMany
Repeater field
You can use a Repeater field and map the relationship query to an array. This might not be a good idea if you have a lot of related items.
Child components
You can have separate forms for each child. Maybe with the "Without buttons" form type (submit on keydown enter) or keep just the save button.
Something like this, in the parent view:
Inline edit
You could also have a div with a click event that hides or shows each child form. Like an "inline edit" thing. It depends on how many related items you will handle att the same time, or use form type "As Modal", to load the child model on a click event.
Last updated
Was this helpful?