BelongsToMany create & edit form UI for Laravel Nova. Enables attaching relationships easily and includes validation.
Forked from dillingham/nova-attach-many.
composer require blendbyte/nova-attach-manyuse NovaAttachMany\AttachMany;
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
];
}You can explicitly define the relationship & Nova resource:
AttachMany::make('Field Name', 'relationshipName', RelatedResource::class);You can pass additional parameters for any pivot value. See the Laravel docs on syncing associations for details.
AttachMany::make('Field Name', 'relationshipName', RelatedResource::class, ['pivot_name' => value]);This package only provides the create/edit views that BelongsToMany does not. Use BelongsToMany for displaying the table on detail views:
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
BelongsToMany::make('Permissions'),
];
}You can set min, max, size or custom rule objects:
->rules('min:5', 'max:10', 'size:10', new CustomRule)| Method | Description |
|---|---|
->showCounts() |
Shows "selected / total" |
->showPreview() |
Shows only selected items |
->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('<b>Tip:</b> help text') |
Set the help text |
The attachable resources will be filtered by relatableQuery(), so you can control which resources are available for attachment.
Add a method to the resource to be notified of sync changes. The method must be a camel-cased version of the attribute name, followed by Synced:
public function fields(Request $request)
{
return [
AttachMany::make('Permissions'),
];
}
public function permissionsSynced(array $changes)
{
$changes['attached']; // IDs of attached models
$changes['detached']; // IDs of detached models
$changes['updated']; // IDs of updated models
}This field respects policies. For example, with a Role / Permission setup:
RolePolicy:attachAnyPermission($user, $role)RolePolicy:attachPermission($user, $role, $permission)PermissionPolicy:viewAny($user)
This project is maintained by Blendbyte — a team of engineers with 20+ years of experience building cloud infrastructure, web applications, and developer tools. We use these packages in production ourselves and actively contribute to the open source ecosystem we rely on every day. Issues and PRs are always welcome.
🌐 blendbyte.com · 📧 hello@blendbyte.com


