BelongsToManyRelation
extends Relation
in package
Implements belongs-to-many relationship logic (many-to-many with pivot table).
Table of Contents
Properties
- $_parent : Model
- $_pdo : PDO
- $_relatedModel : string
- $_foreignPivotKey : string
- $_parentKey : string
- $_pivotTable : string
- $_relatedKey : string
- $_relatedPivotKey : string
Methods
- __construct() : mixed
- Constructor
- attach() : void
- Attach related models to the pivot table.
- detach() : void
- Detach related models from the pivot table.
- eagerLoad() : void
- Eager load relations for multiple models.
- getRelatedModel() : string
- Get the related model class name.
- handleDependent() : void
- Handle dependent cascade when parent is destroyed.
- load() : Model|array<string|int, mixed>|null
- Load the related models.
- sync() : void
- Sync the pivot table with the given IDs.
Properties
$_parent
protected
Model
$_parent
$_pdo
protected
PDO
$_pdo
$_relatedModel
protected
string
$_relatedModel
$_foreignPivotKey
private
string
$_foreignPivotKey
$_parentKey
private
string
$_parentKey
$_pivotTable
private
string
$_pivotTable
$_relatedKey
private
string
$_relatedKey
$_relatedPivotKey
private
string
$_relatedPivotKey
Methods
__construct()
Constructor
public
__construct(PDO $pdo, Model $parent, string $relatedModel, string $pivotTable, string $foreignPivotKey, string $relatedPivotKey[, string $parentKey = 'id' ][, string $relatedKey = 'id' ]) : mixed
Parameters
- $pdo : PDO
-
Database connection
- $parent : Model
-
Parent model instance
- $relatedModel : string
-
Related model class name
- $pivotTable : string
-
Pivot table name
- $foreignPivotKey : string
-
Foreign key in pivot table for parent model
- $relatedPivotKey : string
-
Foreign key in pivot table for related model
- $parentKey : string = 'id'
-
Parent key column name
- $relatedKey : string = 'id'
-
Related key column name
attach()
Attach related models to the pivot table.
public
attach(array<string|int, mixed>|int $ids) : void
Parameters
- $ids : array<string|int, mixed>|int
-
Single ID or array of IDs
detach()
Detach related models from the pivot table.
public
detach([array<string|int, mixed>|int|null $ids = null ]) : void
Parameters
- $ids : array<string|int, mixed>|int|null = null
-
Single ID, array of IDs, or null to detach all
eagerLoad()
Eager load relations for multiple models.
public
eagerLoad(array<string|int, mixed> $models) : void
Parameters
- $models : array<string|int, mixed>
-
Array of parent models
getRelatedModel()
Get the related model class name.
public
getRelatedModel() : string
Return values
stringhandleDependent()
Handle dependent cascade when parent is destroyed.
public
handleDependent(DependentStrategy $strategy) : void
For BelongsToMany relationships:
- Destroy: Deletes related records AND pivot entries
- DeleteAll: Deletes only pivot entries (default behavior)
- Nullify: Same as DeleteAll (deletes pivot entries)
- Restrict: Prevents deletion if pivot entries exist
Parameters
- $strategy : DependentStrategy
Tags
load()
Load the related models.
public
load() : Model|array<string|int, mixed>|null
Return values
Model|array<string|int, mixed>|nullsync()
Sync the pivot table with the given IDs.
public
sync(array<string|int, mixed> $ids) : void
Removes all existing pivot entries and creates new ones.
Parameters
- $ids : array<string|int, mixed>
-
Array of related model IDs