Relationship Fields

Translatable v0.2.0

This document applies to all types of relationships.

These fields extend Select Fields.

Note: all of the fields mentioned here are in the namespace Yadda\Enso\Crud\Forms\Fields.

Which field to use

You should generally pick the same field as the type of relationship you are using. These include:

  • BelongsToField
  • BelongsToManyField
  • HasManyField
  • HasOneField

If you need the order of these relationships is important you can use one of these fields:

  • OrderableBelongsToManyField
  • OrderableHasManyField
  • OrderableMultiSelectField
  • OrderableRelationshipField

The following aren't currently available as CRUD fields.

  • HasManyThrough
  • HasOneOrMany
  • MorphMany
  • MorphOne
  • MorphOneOrMany
  • MorphTo
  • MorphToMany

These fields extend the following fields, which you can use directly if your field is plain data and not a relationship.

  • SelectField
  • MultiSelectField

All relationship fields are base on the RelationshipField class which you shouldn't use directly.

  • RelationshipField

There are also repeater fields. They exist.

  • BelongsToManyRepeaterField
  • HasManyRepeaterField
  • RelationshipRepeaterField

Example usage with provided options:

(new BelongsToField('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

(new BelongsToManyField('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.