code16/sharp
Code-driven CMS framework for Laravel (PHP 8.3+/Laravel 11+). Build admin/CMS sections with a clean UI and strong DX: CRUD with validation, search/sort/filter, bulk or custom commands, and authorization—no front-end code required, data-agnostic.
Class: Code16\Sharp\Form\Fields\SharpFormAutocompleteListField
This one may seem a little strange. The goal is to build a List with only one field in each item: an Autocomplete.
First let's see a use case: imagine you want to handle a list of winners by selecting them in a big list of Players, for which a remote Autocomplete is the best choice (otherwise you could have opted for a Tags Field).
You can in fact define the list as this:
SharpFormAutocompleteListField::make('winners')
->setLabel('Winners')
->setItemField(
SharpFormAutocompleteRemoteField::make('item')
->setRemoteEndpoint('/players')
// [...]
)
);
::: tip
The key of the Autocomplete, item here, could be anything you want, as soon you stay consistent in the buildFormLayout() part.
:::
But why can't we use a classic List for this? Well, the model->winners relation is N-N, here (belongsToMany), but Lists are meant to handle 1-N relationships (hasMany). Here we want one field, the Autocomplete, to represent the whole item object.
Configuration is the same as the classic List, except for:
setItemField(SharpFormAutocompleteRemoteField $field)You can use this function instead of addItemField, since items of AutocompleteList have only one field.
addItemField(SharpFormField $field)This method is an alias for setItemField(), meaning that you can only pass an Autocomplete, and it can only be called once.
toFrontWell, you must provide an array or Collection (same as for a List, see related documentation) of models with at least attributes designated by templates for the Autocomplete (see related documentation).
formFrontReturns the selected item id.
How can I help you explore Laravel packages today?