trait InteractsWithPivotTable

Methods

array
toggle(mixed $ids, bool $touch = true)

Toggles a model (or models) from the parent.

array
toggleOrFail(mixed $ids, bool $touch = true)

Toggle a model (or models) from the parent within a transaction.

array
syncWithoutDetaching(Collection|Model|array|int|string|null $ids)

Sync the intermediate tables with a list of IDs without detaching.

array
sync(Collection|Model|array|int|string|null $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models.

array
syncOrFail(Collection|Model|array|int|string|null $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models within a transaction.

array
syncWithoutDetachingOrFail(Collection|Model|array|int|string|null $ids)

Sync the intermediate tables with a list of IDs without detaching within a transaction.

array
syncWithPivotValues(Collection|Model|array|int|string|null $ids, array $values, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models with the given pivot values.

array
syncWithPivotValuesOrFail(Collection|Model|array|int|string|null $ids, array $values, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models with the given pivot values within a transaction.

array
formatRecordsList(array $records)

Format the sync / toggle record list so that it is keyed by ID.

array
attachNew(array $records, array $current, bool $touch = true)

Attach all of the records that aren't in the given current records.

int
updateExistingPivot(mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table.

int
updateExistingPivotOrFail(mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table within a transaction.

int
updateExistingPivotUsingCustomClass(mixed $id, array $attributes, bool $touch)

Update an existing pivot record on the table via a custom class.

void
attach(mixed $ids, array $attributes = [], bool $touch = true)

Attach a model to the parent.

void
attachOrFail(mixed $ids, array $attributes = [], bool $touch = true)

Attach a model to the parent within a transaction.

void
attachUsingCustomClass(mixed $ids, array $attributes)

Attach a model to the parent using a custom class.

array
formatAttachRecords(array $ids, array $attributes)

Create an array of records to insert into the pivot table.

array
formatAttachRecord(int|string $key, mixed $value, array $attributes, bool $hasTimestamps)

Create a full attachment record payload.

array
extractAttachIdAndAttributes(mixed $key, mixed $value, array $attributes)

Get the attach record ID and extra attributes.

array
baseAttachRecord(mixed $id, bool $timed)

Create a new pivot attachment record.

array
addTimestampsToAttachment(array $record, bool $exists = false)

Set the creation and update timestamps on an attach record.

bool
hasPivotColumn(string|null $column)

Determine whether the given column is defined as a pivot column.

int
detach(mixed $ids = null, bool $touch = true)

Detach models from the relationship.

int
detachOrFail(mixed $ids = null, bool $touch = true)

Detach models from the relationship within a transaction.

int
detachUsingCustomClass(mixed $ids)

Detach models from the relationship using a custom class.

getCurrentlyAttachedPivots()

Get the pivot models that are currently attached.

getCurrentlyAttachedPivotsForIds(mixed $ids = null)

Get the pivot models that are currently attached, filtered by related model keys.

newPivot(array $attributes = [], bool $exists = false)

Create a new pivot model instance.

newExistingPivot(array $attributes = [])

Create a new existing pivot model instance.

newPivotStatement()

Get a new plain query builder for the pivot table.

getPivotConnection()

Get the connection that owns the pivot table.

newPivotStatementForId(mixed $id)

Get a new pivot statement for a given "other" ID.

newPivotQuery()

Create a new query builder for the pivot table.

bool
hasPivotConstraints()

Determine whether the relation has pivot constraints.

withPivot(mixed $columns)

Set the columns on the pivot table to retrieve.

array
parseIds(mixed $value)

Get all of the IDs from the given mixed value.

mixed
parseId(mixed $value)

Get the ID from the given mixed value.

array
castKeys(array $keys)

Cast the given keys to integers if they are numeric and string otherwise.

mixed
castKey(mixed $key)

Cast the given key to convert to primary key type.

array
castAttributes(array $attributes)

Cast the given pivot attributes.

mixed
getTypeSwapValue(string $type, mixed $value)

Converts a given value to a given type value.

Details

at line 24
array toggle(mixed $ids, bool $touch = true)

Toggles a model (or models) from the parent.

Each existing model is detached, and non existing ones are attached.

Parameters

mixed $ids
bool $touch

Return Value

array

at line 73
array toggleOrFail(mixed $ids, bool $touch = true)

Toggle a model (or models) from the parent within a transaction.

Parameters

mixed $ids
bool $touch

Return Value

array

Exceptions

Throwable

at line 83
array syncWithoutDetaching(Collection|Model|array|int|string|null $ids)

Sync the intermediate tables with a list of IDs without detaching.

Parameters

Collection|Model|array|int|string|null $ids

Return Value

array

at line 93
array sync(Collection|Model|array|int|string|null $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models.

Parameters

Collection|Model|array|int|string|null $ids
bool $detaching

Return Value

array

at line 152
array syncOrFail(Collection|Model|array|int|string|null $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models within a transaction.

Parameters

Collection|Model|array|int|string|null $ids
bool $detaching

Return Value

array

Exceptions

Throwable

at line 164
array syncWithoutDetachingOrFail(Collection|Model|array|int|string|null $ids)

Sync the intermediate tables with a list of IDs without detaching within a transaction.

Parameters

Collection|Model|array|int|string|null $ids

Return Value

array

Exceptions

Throwable

at line 174
array syncWithPivotValues(Collection|Model|array|int|string|null $ids, array $values, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models with the given pivot values.

Parameters

Collection|Model|array|int|string|null $ids
array $values
bool $detaching

Return Value

array

at line 188
array syncWithPivotValuesOrFail(Collection|Model|array|int|string|null $ids, array $values, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models with the given pivot values within a transaction.

Parameters

Collection|Model|array|int|string|null $ids
array $values
bool $detaching

Return Value

array

Exceptions

Throwable

at line 196
protected array formatRecordsList(array $records)

Format the sync / toggle record list so that it is keyed by ID.

Parameters

array $records

Return Value

array

at line 212
protected array attachNew(array $records, array $current, bool $touch = true)

Attach all of the records that aren't in the given current records.

Parameters

array $records
array $current
bool $touch

Return Value

array

at line 241
int updateExistingPivot(mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

int

at line 267
int updateExistingPivotOrFail(mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table within a transaction.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

int

Exceptions

Throwable

at line 275
protected int updateExistingPivotUsingCustomClass(mixed $id, array $attributes, bool $touch)

Update an existing pivot record on the table via a custom class.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

int

at line 295
void attach(mixed $ids, array $attributes = [], bool $touch = true)

Attach a model to the parent.

Parameters

mixed $ids
array $attributes
bool $touch

Return Value

void

at line 319
void attachOrFail(mixed $ids, array $attributes = [], bool $touch = true)

Attach a model to the parent within a transaction.

Parameters

mixed $ids
array $attributes
bool $touch

Return Value

void

Exceptions

Throwable

at line 327
protected void attachUsingCustomClass(mixed $ids, array $attributes)

Attach a model to the parent using a custom class.

Parameters

mixed $ids
array $attributes

Return Value

void

at line 342
protected array formatAttachRecords(array $ids, array $attributes)

Create an array of records to insert into the pivot table.

Parameters

array $ids
array $attributes

Return Value

array

at line 367
protected array formatAttachRecord(int|string $key, mixed $value, array $attributes, bool $hasTimestamps)

Create a full attachment record payload.

Parameters

int|string $key
mixed $value
array $attributes
bool $hasTimestamps

Return Value

array

at line 380
protected array extractAttachIdAndAttributes(mixed $key, mixed $value, array $attributes)

Get the attach record ID and extra attributes.

Parameters

mixed $key
mixed $value
array $attributes

Return Value

array

at line 390
protected array baseAttachRecord(mixed $id, bool $timed)

Create a new pivot attachment record.

Parameters

mixed $id
bool $timed

Return Value

array

at line 413
protected array addTimestampsToAttachment(array $record, bool $exists = false)

Set the creation and update timestamps on an attach record.

Parameters

array $record
bool $exists

Return Value

array

at line 437
bool hasPivotColumn(string|null $column)

Determine whether the given column is defined as a pivot column.

Parameters

string|null $column

Return Value

bool

at line 445
int detach(mixed $ids = null, bool $touch = true)

Detach models from the relationship.

Parameters

mixed $ids
bool $touch

Return Value

int

at line 483
int detachOrFail(mixed $ids = null, bool $touch = true)

Detach models from the relationship within a transaction.

Parameters

mixed $ids
bool $touch

Return Value

int

Exceptions

Throwable

at line 491
protected int detachUsingCustomClass(mixed $ids)

Detach models from the relationship using a custom class.

Parameters

mixed $ids

Return Value

int

at line 500
protected Collection getCurrentlyAttachedPivots()

Get the pivot models that are currently attached.

Return Value

Collection

at line 508
protected Collection getCurrentlyAttachedPivotsForIds(mixed $ids = null)

Get the pivot models that are currently attached, filtered by related model keys.

Parameters

mixed $ids

Return Value

Collection

at line 522
Model newPivot(array $attributes = [], bool $exists = false)

Create a new pivot model instance.

Parameters

array $attributes
bool $exists

Return Value

Model

at line 559
Model newExistingPivot(array $attributes = [])

Create a new existing pivot model instance.

Parameters

array $attributes

Return Value

Model

at line 567
Builder newPivotStatement()

Get a new plain query builder for the pivot table.

Return Value

Builder

at line 575
protected ConnectionInterface getPivotConnection()

Get the connection that owns the pivot table.

Return Value

ConnectionInterface

at line 583
Builder newPivotStatementForId(mixed $id)

Get a new pivot statement for a given "other" ID.

Parameters

mixed $id

Return Value

Builder

at line 591
Builder newPivotQuery()

Create a new query builder for the pivot table.

Return Value

Builder

at line 609
protected bool hasPivotConstraints()

Determine whether the relation has pivot constraints.

Return Value

bool

at line 619
InteractsWithPivotTable withPivot(mixed $columns)

Set the columns on the pivot table to retrieve.

Parameters

mixed $columns

Return Value

InteractsWithPivotTable

at line 632
protected array parseIds(mixed $value)

Get all of the IDs from the given mixed value.

Parameters

mixed $value

Return Value

array

at line 654
protected mixed parseId(mixed $value)

Get the ID from the given mixed value.

Parameters

mixed $value

Return Value

mixed

at line 662
protected array castKeys(array $keys)

Cast the given keys to integers if they are numeric and string otherwise.

Parameters

array $keys

Return Value

array

at line 672
protected mixed castKey(mixed $key)

Cast the given key to convert to primary key type.

Parameters

mixed $key

Return Value

mixed

at line 683
protected array castAttributes(array $attributes)

Cast the given pivot attributes.

Parameters

array $attributes

Return Value

array

at line 693
protected mixed getTypeSwapValue(string $type, mixed $value)

Converts a given value to a given type value.

Parameters

string $type
mixed $value

Return Value

mixed