https://github.com/dillingham/nova-attach-many
BelongsToMany create / edit form component for Nova
https://github.com/dillingham/nova-attach-many
laravel laravel-nova
Last synced: 6 days ago
JSON representation
BelongsToMany create / edit form component for Nova
- Host: GitHub
- URL: https://github.com/dillingham/nova-attach-many
- Owner: dillingham
- License: mit
- Created: 2019-02-02T05:58:23.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-10T14:37:20.000Z (over 1 year ago)
- Last Synced: 2024-03-14T14:45:32.050Z (11 months ago)
- Topics: laravel, laravel-nova
- Language: PHP
- Homepage:
- Size: 396 KB
- Stars: 271
- Watchers: 7
- Forks: 104
- Open Issues: 21
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# Nova Attach Many
[](https://packagist.org/packages/dillingham/nova-attach-many)
[](https://packagist.org/packages/dillingham/nova-attach-many) [](https://twitter.com/im_brian_d)Belongs To Many create & edit form UI for Nova. Enables attaching relationships easily and includes validation.

### Installation
```bash
composer require dillingham/nova-attach-many
```### Usage
```php
use NovaAttachMany\AttachMany;
```
```php
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
];
}
```You can explicitly define the relationship & Nova resource:
```php
AttachMany::make('Field Name', 'relationshipName', RelatedResource::class);
```### Display on detail:
This package only provides the create / edit views that BelongsToMany does not.
BelongsToMany should be used for displaying the table on detail views.
```php
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
BelongsToMany::make('Permissions'),
];
}
```### Validation
You can set min, max, size or custom rule objects
```php
->rules('min:5', 'max:10', 'size:10', new CustomRule)
```
### Options
Here are a few customization options
- `->showCounts()` Shows "selected/total"
- `->showPreview()` Shows only selected
- `->hideToolbar()` Removes search & select all
- `->height('500px')` Set custom height
- `->fullWidth()` Set to full width
- `->showRefresh()` Request the resources again
- `->showSubtitle()` Show the resource's subtitle
- `->help('Tip: help text')` Set the help text### All Options Demo
### Relatable
The attachable resources will be filtered by relatableQuery()
So you can filter which resources are able to be attached### Being Notified of Changes
You can add a method to the resource to be notified of the changes that have happened:
The method must be a camel cased version of the attribute name, followed by `Synced`. For example:
```php
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
];
}
``````php
public function permissionsSynced(array $changes)
{
$changes['attached']; // An array of IDs of attached models
$changes['detached']; // An array of IDs of detached models
$changes['updated']; // An array of IDs of updated models
}
```### Authorization
This field also respects policies: ie Role / Permission
- RolePolicy: attachAnyPermission($user, $role)
- RolePolicy: attachPermission($user, $role, $permission)
- PermissionPolicy: viewAny($user)---
# Author
Hi 👋, Im Brian D. I created this Nova package [and others](https://novapackages.com/collaborators/dillingham)
Hope you find it useful. Feel free to reach out with feedback.
Follow me on twitter: [@im_brian_d](https://twitter.com/im_brian_d)