In addition to the common fields, Lamotivo supports all of Laravel's relationships.
The HasOne field corresponds to a hasOne Eloquent relationship.
use Anyspin\Lamotivo\Fields\HasOne;
HasOne::make(Address::class);The first argument is a Lamotivo resource class name. Lamotivo will humanize the displayable name and determine automatically the underlying attribute name. You may pass the displayable name and attribute name as the second and third arguments accordingly:
HasOne::make(Address::class, 'Destination', 'address_id');The HasMany field corresponds to a hasMany Eloquent relationship.
use Anyspin\Lamotivo\Fields\HasMany;
HasMany::make(Article::class);Once the field has been added to your resource, it will be displayed on the resource's details screen.
The BelongsTo field corresponds to a belongsTo Eloquent relationship.
use Anyspin\Lamotivo\Fields\BelongsTo;
BelongsTo::make(Cart::class);When a BelongsTo field is shown on resource creation or update forms, a drop-down list will display the title of the resource. To customize the title of a resource, you may define a title property on the resource class:
public static $title = 'name';The BelongsToMany field corresponds to a belongsToMany Eloquent relationship.
use Anyspin\Lamotivo\Fields\BelongsToMany;
BelongsToMany::make(Ingredient::class);Once the field has been added to your resource, it will be displayed on the resource's details screen.
If your belongsToMany relationship interacts with additional pivot fields, you may also attach those to your BelongsToMany Lamotivo relationship.
BelongsToMany::make(Ingredient::class)
->fields(function () {
return [
Text::make('amount'),
];
});When a BelongsToMany field is shown on resource creation or update forms, a drop-down list will display the title of the resource. To customize the title of a resource, you may define a title property on the resource class:
public static $title = 'name';The MorphOne field corresponds to a morphOne Eloquent relationship.
use Anyspin\Lamotivo\Fields\MorphOne;
MorphOne::make(Image::class);The MorphMany field corresponds to a morphMany Eloquent relationship.
use Anyspin\Lamotivo\Fields\MorphMany;
MorphMany::make(Comment::class);The MorphTo field corresponds to a morphTo Eloquent relationship.
use Anyspin\Lamotivo\Fields\MorphTo;
MorphTo::make('commentable')->types([
Post::class,
Video::class,
]);The first argument is a basic column name, it will be tranformed to _id and _type column names. You may pass a label as the second argument.
The type method is used to instruct MorphTo what resources it may be associated with.
The MorphToMany field corresponds to a morphToMany Eloquent relationship.
use Anyspin\Lamotivo\Fields\MorphToMany;
MorphToMany::make(Tag::class);Once the field has been added to your resource, it will be displayed on the resource's details screen.
The behaviour of this field type is quite equal to BelongsToMany.
By default, when the BelongsTo, MorphTo, and MorphToMany relationship fields are shown on resource creation or update forms, a simple drop-down list will be displayed.
When you operate with a sufficiently large number of records, you may want to organize the list with a search input control. To use it, just chain searchable method onto the field's definition:
BelongsTo::make(Tag::class)
->searchable();