outl1ne/nova-multiselect-field
Installation:
composer require outl1ne/nova-multiselect-field
Publish the package assets (if needed):
php artisan vendor:publish --provider="Outl1ne\MultiselectField\MultiselectFieldServiceProvider"
First Use Case: Add the field to a Nova resource:
use Outl1ne\MultiselectField\Multiselect;
public function fields(Request $request)
{
return [
Multiselect::make('Tags')
->options(['Option 1', 'Option 2', 'Option 3'])
->displayUsing(fn ($value) => $value ?? 'No tags'),
];
}
Database Consideration:
Ensure your model uses a string/text column (e.g., tags) to store JSON-encoded arrays.
Dynamic Options via Closure: Fetch options asynchronously (e.g., from a database):
Multiselect::make('Categories')
->options(function () {
return Category::query()->pluck('name', 'id');
})
->searchable()
Dependency Between Fields: Chain multiselects to filter options dynamically:
Multiselect::make('Subcategories')
->options(function () {
return Subcategory::where('category_id', $this->parent->categories)->pluck('name', 'id');
})
->dependsOn('categories')
Reordering with Drag & Drop: Enable sorting for a list of items:
Multiselect::make('Priority Tags')
->options(['Urgent', 'High', 'Medium', 'Low'])
->sortable()
Display Formatting: Customize how values appear in detail/index views:
Multiselect::make('Skills')
->options(Skill::all()->pluck('name', 'id'))
->displayUsing(fn ($values) => implode(', ', $values ?? []))
array, required) on the underlying column.Database Storage:
["option1", "option2"]). Ensure your column type (e.g., text) can accommodate this.protected $casts = [
'tags' => 'json',
];
Search Performance:
->searchable()) may slow down if the options closure is expensive.->options(function () {
return Cache::remember('multiselect-options', now()->addHours(1), function () {
return Category::pluck('name', 'id');
});
})
Dependency Conflicts:
dependsOn fields are not populated, the dependent multiselect may fail silently.->default([]) to provide fallback values.Dark Mode Compatibility:
config/nova.php) to log field-related errors.dd(json_decode($model->tags, true));
Custom Templates: Override the field’s Blade template by publishing and modifying:
php artisan vendor:publish --tag=nova-multiselect-field-views
Event Hooks:
Listen for field-specific events (e.g., multiselect.saving) via Nova’s events.
Localization: Extend translations by publishing the language files:
php artisan vendor:publish --tag=nova-multiselect-field-lang
How can I help you explore Laravel packages today?