<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
optimistdigital / nova-multiselect-field example snippets
use Outl1ne\MultiselectField\Multiselect;
public function fields(Request $request)
{
return [
Multiselect::make('Football teams')
->options([
'liverpool' => 'Liverpool FC',
'tottenham' => 'Tottenham Hotspur',
])
// Optional:
->placeholder('Choose football teams') // Placeholder text
->max(4) // Maximum number of items the user can choose
->saveAsJSON() // Saves value as JSON if the database column is of JSON type
->optionsLimit(5) // How many items to display at once
->reorderable() // Allows reordering functionality
->singleSelect() // If you want a searchable single select field
->distinct('football') // Disables values used by other multiselects in same distinct group
->taggable() // Possible to add values ("tags") on the fly
// Async model querying
Multiselect::make('Artists')
->asyncResource(Artist::class),
// If you want a custom search, create your own endpoint:
->api('/api/multiselect/artists?something=false', Artist::class),
];
}
// Add your BelongsToMany relationship to your model:
public function categories()
{
return $this->belongsToMany(\App\Models\Category::class);
}
// Add the field to your Resource for asynchronous option querying:
Multiselect::make('Categories', 'categories')
->belongsToMany(\App\Nova\Resources\Category::class),
// Alternatively, you can set the second argument to 'false' to
// query the options on page load and prevent the user from having
// to first type in order to view the available options. Note: Not
// recommended for unbounded relationship row counts.
Multiselect::make('Categories', 'categories')
->belongsToMany(\App\Nova\Resources\Category::class, false),