ostheneo/belongstomany is a Laravel package for a laravel nova field..
It currently has 0 GitHub stars and 0 downloads on Packagist.
Install it with composer require ostheneo/belongstomany.
Discover more Laravel packages by ostheneo
or browse all Laravel packages to compare alternatives.
Last updated
A custom BelongsToMany field for Laravel Nova that provides a multiselect interface with server-side search support, designed for handling large datasets efficiently.
nova-components directorycomposer.json:{
"repositories": [
{
"type": "path",
"url": "./nova-components/Belongstomany"
}
],
"require": {
"ostheneo/belongstomany": "*"
}
}
composer update
use Ostheneo\Belongstomany\Belongstomany;
public function fields(NovaRequest $request): array
{
return [
Belongstomany::make('Tags', 'tags', Tag::class)
->optionsLabel('name'),
];
}
Belongstomany::make('Tags', 'tags', Tag::class)
->optionsLabel('name')
->searchable()
->optionsLimit(50),
Belongstomany::make('Tags', 'tags', Tag::class)
->optionsLabel('name')
->searchable()
->optionsLimit(50)
->viewable()
->rules('required'),
optionsLabel(string $field)Specifies which field to display as the label in the multiselect dropdown.
->optionsLabel('name') // Will display the 'name' field of related models
searchable(bool $searchable = true)Enables server-side search. When enabled, the field will fetch options from the server based on user input instead of loading all options upfront.
->searchable() // Enable server-side search
optionsLimit(int $limit)Sets the maximum number of options to load. Works both with and without searchable():
searchable(): Limits results returned per search querysearchable(): Limits initial options loaded (uses local filtering)->optionsLimit(50) // Load maximum 50 options
viewable(bool $viewable = true)When enabled, selected items will be clickable links to their detail pages.
->viewable() // Make selected items clickable
rules($rules)Apply validation rules to the field.
->rules('required')
->rules(['required', 'min:1'])
setPivot(array $attributes)Set additional pivot table attributes when syncing relationships.
->setPivot(['added_by' => auth()->id()])
setMultiselectProps(array $props)Customize vue-multiselect component labels.
->setMultiselectProps([
'selectLabel' => 'Press enter to select',
'deselectLabel' => 'Press enter to remove',
'selectedLabel' => 'Selected',
])
setMultiselectSlots(array $slots)Customize empty state messages.
->setMultiselectSlots([
'noOptions' => 'No items available',
'noResult' => 'No matching items found',
])
showAsListInDetail(bool $show = true)Display selected items as a list on the detail page.
->showAsListInDetail()
Your related model must have the standard BelongsToMany relationship defined:
// App\Models\Post.php
public function tags()
{
return $this->belongsToMany(Tag::class);
}
And the inverse relationship (optional but recommended):
// App\Models\Tag.php
public function posts()
{
return $this->belongsToMany(Post::class);
}
When searchable() is enabled, the field performs server-side searches with the following behavior:
optionsLimit recordsThe search is performed on the field specified in optionsLabel().
The field uses Nova's CSS variables for consistent theming:
--colors-primary-500 / --colors-primary-600 for selected tags--colors-gray-* for borders and backgroundsMIT License