Relationship Fields
This document applies to all types of relationships.
These fields extend Select Fields.
Which field to use
You should generally pick the same field as the type of relationship you are using. These include:
BelongsToFieldBelongsToManyFieldHasManyFieldHasOneField
If you need the order of these relationships is important you can use one of these fields:
OrderableBelongsToManyFieldOrderableHasManyFieldOrderableMultiSelectFieldOrderableRelationshipField
The following aren't currently available as CRUD fields.
HasManyThroughHasOneOrManyMorphManyMorphOneMorphOneOrManyMorphToMorphToMany
These fields extend the following fields, which you can use directly if your field is plain data and not a relationship.
SelectFieldMultiSelectField
All relationship fields are base on the RelationshipField class which you shouldn't use directly.
RelationshipField
There are also repeater fields. They exist.
BelongsToManyRepeaterFieldHasManyRepeaterFieldRelationshipRepeaterField
Example usage with provided options:
BelongsToField::make('parentThing') // Name of relationship
->setLabel('Related Thing')
->setOptions(Thing::published()), // Only allow picking published Things, published being a query scope.
Example usage for ajax options
BelongsToManyField::make('things')
->setLabel('Many Things')
->useAjax(route('admin.things.index'), Thing::class)
->setSettings([
'label' => 'title' // For Models without a 'name' column
'searchable' => true,
'max' => 5
]),
For basic setups you can provide the route to the index method of the crud you are searching, e.g. route('admin.things.index'). You can set which fields will be searched by going to the CRUD config that is being searched (in the example above it would be Thing) and using $this->setSearchColumns(['some_column_name']) in the configure() method.
For more control you can point it at your own route. When searching, search, order_by and order parameters will be provided.
Options
You can use the setOptions method to set an array that will be passed to vue-multiselect.
See Select Fields for more information.