Skip to main content
Rest Generic Class provides a powerful trait for managing many-to-many relationships with full CRUD support, pivot data, and complex filtering. This guide covers the ManagesManyToMany trait for controllers that expose belongsToMany relationships.

Overview

The trait provides:
  • List and show related entities with filtering, pagination, and ordering
  • Create and update related models through the relationship
  • Delete related models (with optional cascade)
  • Attach/detach existing entities (pivot-only operations)
  • Sync the entire relationship set
  • Toggle specific IDs
  • Update pivot fields without modifying the related model
  • Export related entities to Excel/PDF

Setup

Configuration Reference

Required Fields

Mutation Config (Optional)

List all addresses for a user:
Response:

With Filtering

With Pagination

With Ordering

Response:

Attach Operations

Attach operations link existing entities without creating new ones.

Single Attach

Attach address ID 5 with pivot data:
Response:

Bulk Attach

Attach multiple addresses at once:
Response:

Sync Operation

Sync replaces the entire relationship set. IDs not in the sync payload are detached.

Sync with ID Array

Result:
  • User 42 now has exactly 3 addresses (1, 2, 3)
  • Any other addresses are detached
  • No pivot data is updated

Sync with Objects

Sync with Laravel Map Format

Response (all formats):

Toggle Operation

Toggle reverses the attachment status: attached IDs become detached, detached IDs become attached.
Before:
  • User has addresses: [1, 5, 7]
After toggle:
  • User has addresses: [2, 3, 5, 7]
  • 1 was detached (was attached)
  • 2 and 3 were attached (were detached)
  • 5 and 7 unchanged (not in toggle list)
Response:

Detach Operations

Single Detach

Remove the pivot row (keeps the Address model):
Response:

Bulk Detach

Response:

Update Pivot Fields

Update pivot data without changing the related model.

Single Pivot Update

Response:

Bulk Pivot Update

Pivot Column Whitelist

The pivotColumns config provides a security whitelist:

How It Works

With the whitelist above: Request:
Actual pivot data stored:
approved_at and internal_notes are silently stripped (not in whitelist).
When pivotColumns is empty or not set, all pivot columns are accepted (backward compatible).
Create new related entities through the relationship.

Create Single

Response (201 Created):

Create Bulk

By default, deleting a relation also deletes the related model.

Single Delete

This:
  1. Detaches the address from user 42
  2. Deletes the Address model (if deleteRelated=true)
Response:

Pivot-Only Removal

To keep the Address model and only remove the pivot row, configure:
Now DELETE only removes the pivot row.

Export to Excel

Optional parameters:
  • columns: Specify columns to export
  • select, oper, orderby: Apply filters before export

Export to PDF

Real-World Examples

User Addresses (CRM)

UserController.php

Product Tags (E-commerce)

ProductController.php

Course Enrollments (LMS)

CourseController.php

Next Steps

Relation Loading

Learn eager loading for many-to-many relationships

Bulk Operations

Optimize bulk attach/detach operations

Permissions

Secure many-to-many endpoints with Spatie

API Reference

Complete many-to-many API reference

Evidence

  • File: src/Core/Traits/ManagesManyToMany.php
    Lines: 16-1191 (entire file)
    Implements all many-to-many operations including listRelation, attachRelation, detachRelation, updatePivotRelation
  • File: documentacion/doc-en/03-usage/05-many-to-many.md
    Lines: 1-391
    Complete documentation of the trait with examples and configuration