PHPackages                             ostheneo/belongstomany - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. ostheneo/belongstomany

ActiveLibrary[Admin Panels](/categories/admin)

ostheneo/belongstomany
======================

A Laravel Nova field.

00PHP

Since Jan 20Pushed 3mo agoCompare

[ Source](https://github.com/OsTheNeo/belongstomany)[ Packagist](https://packagist.org/packages/ostheneo/belongstomany)[ RSS](/packages/ostheneo-belongstomany/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

BelongsToMany Field for Laravel Nova
====================================

[](#belongstomany-field-for-laravel-nova)

A custom BelongsToMany field for Laravel Nova that provides a multiselect interface with server-side search support, designed for handling large datasets efficiently.

Features
--------

[](#features)

- **Server-side search**: Search through thousands of records without loading them all
- **Debounced search**: 300ms debounce to prevent excessive API calls
- **Preserves selections**: Selected items remain visible while searching
- **Nova theme integration**: Uses Nova CSS variables for consistent styling
- **Dark mode support**: Fully compatible with Nova's dark mode
- **Dependent fields support**: Works with Nova's dependent fields feature

Installation
------------

[](#installation)

1. Place the package in your `nova-components` directory
2. Register the package in your `composer.json`:

```
{
    "repositories": [
        {
            "type": "path",
            "url": "./nova-components/Belongstomany"
        }
    ],
    "require": {
        "ostheneo/belongstomany": "*"
    }
}
```

3. Run composer update:

```
composer update
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Ostheneo\Belongstomany\Belongstomany;

public function fields(NovaRequest $request): array
{
    return [
        Belongstomany::make('Tags', 'tags', Tag::class)
            ->optionsLabel('name'),
    ];
}
```

### With Server-Side Search (Recommended for large datasets)

[](#with-server-side-search-recommended-for-large-datasets)

```
Belongstomany::make('Tags', 'tags', Tag::class)
    ->optionsLabel('name')
    ->searchable()
    ->optionsLimit(50),
```

### Full Example

[](#full-example)

```
Belongstomany::make('Tags', 'tags', Tag::class)
    ->optionsLabel('name')
    ->searchable()
    ->optionsLimit(50)
    ->viewable()
    ->rules('required'),
```

Available Methods
-----------------

[](#available-methods)

### `optionsLabel(string $field)`

[](#optionslabelstring-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)`

[](#searchablebool-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)`

[](#optionslimitint-limit)

Sets the maximum number of options to load. Works both with and without `searchable()`:

- **With `searchable()`**: Limits results returned per search query
- **Without `searchable()`**: Limits initial options loaded (uses local filtering)

```
->optionsLimit(50)  // Load maximum 50 options
```

### `viewable(bool $viewable = true)`

[](#viewablebool-viewable--true)

When enabled, selected items will be clickable links to their detail pages.

```
->viewable()  // Make selected items clickable
```

### `rules($rules)`

[](#rulesrules)

Apply validation rules to the field.

```
->rules('required')
->rules(['required', 'min:1'])
```

### `setPivot(array $attributes)`

[](#setpivotarray-attributes)

Set additional pivot table attributes when syncing relationships.

```
->setPivot(['added_by' => auth()->id()])
```

### `setMultiselectProps(array $props)`

[](#setmultiselectpropsarray-props)

Customize vue-multiselect component labels.

```
->setMultiselectProps([
    'selectLabel' => 'Press enter to select',
    'deselectLabel' => 'Press enter to remove',
    'selectedLabel' => 'Selected',
])
```

### `setMultiselectSlots(array $slots)`

[](#setmultiselectslotsarray-slots)

Customize empty state messages.

```
->setMultiselectSlots([
    'noOptions' => 'No items available',
    'noResult' => 'No matching items found',
])
```

### `showAsListInDetail(bool $show = true)`

[](#showaslistindetailbool-show--true)

Display selected items as a list on the detail page.

```
->showAsListInDetail()
```

Model Requirements
------------------

[](#model-requirements)

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);
}
```

Server-Side Search
------------------

[](#server-side-search)

When `searchable()` is enabled, the field performs server-side searches with the following behavior:

1. **Initial load**: Fetches up to `optionsLimit` records
2. **On search**: Sends search query to server with 300ms debounce
3. **Preserves selections**: Currently selected items remain visible even if not in search results

The search is performed on the field specified in `optionsLabel()`.

Styling
-------

[](#styling)

The field uses Nova's CSS variables for consistent theming:

- `--colors-primary-500` / `--colors-primary-600` for selected tags
- `--colors-gray-*` for borders and backgrounds
- Automatic dark mode support

License
-------

[](#license)

MIT License

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance53

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5789902?v=4)[Harry Gutierrez](/maintainers/OsTheNeo)[@OsTheNeo](https://github.com/OsTheNeo)

---

Top Contributors

[![OsTheNeo](https://avatars.githubusercontent.com/u/5789902?v=4)](https://github.com/OsTheNeo "OsTheNeo (9 commits)")[![claude](https://avatars.githubusercontent.com/u/81847?v=4)](https://github.com/claude "claude (1 commits)")

### Embed Badge

![Health badge](/badges/ostheneo-belongstomany/health.svg)

```
[![Health](https://phpackages.com/badges/ostheneo-belongstomany/health.svg)](https://phpackages.com/packages/ostheneo-belongstomany)
```

###  Alternatives

[jeroennoten/laravel-adminlte

Easy AdminLTE integration with Laravel

4.0k4.8M43](/packages/jeroennoten-laravel-adminlte)[dmstr/yii2-adminlte-asset

AdminLTE backend theme asset bundle for Yii 2.0 Framework

1.1k1.8M67](/packages/dmstr-yii2-adminlte-asset)[dwij/laraadmin

LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like CRUD Generation, Module Manager, Media, Menus, Backups and much more

1.6k68.7k](/packages/dwij-laraadmin)[filament/spatie-laravel-media-library-plugin

Filament support for `spatie/laravel-medialibrary`.

1764.8M125](/packages/filament-spatie-laravel-media-library-plugin)[bezhansalleh/filament-exceptions

A Simple &amp; Beautiful Pluggable Exception Viewer for FilamentPHP's Admin Panel

193195.9k13](/packages/bezhansalleh-filament-exceptions)[filament/infolists

Easily add beautiful read-only infolists to any Livewire component.

1220.8M36](/packages/filament-infolists)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
