trait InteractsWithPivotTable (View source)

Methods

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

Toggles a model (or models) from the parent.

array{attached: array, detached: array, updated: array}
syncWithoutDetaching(Collection|Model|array $ids)

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

array{attached: array, detached: array, updated: array}
sync(Collection|Model|array $ids, bool $detaching = true)

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

array{attached: array, detached: array, updated: array}
syncWithPivotValues(Collection|Model|array $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
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
updateExistingPivotUsingCustomClass(mixed $id, array $attributes, bool $touch)

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

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

Attach a model to the parent.

void
attachUsingCustomClass(mixed $id, 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 $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(int $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 $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
detachUsingCustomClass(mixed $ids)

Detach models from the relationship using a custom class.

getCurrentlyAttachedPivots()

Get the pivot models that are currently attached.

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.

newPivotStatementForId(mixed $id)

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

newPivotQuery()

Create a new query builder for the pivot table.

$this
withPivot(array|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

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

array{attached: array, detached: array, updated: array} syncWithoutDetaching(Collection|Model|array $ids)

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

Parameters

Collection|Model|array $ids

Return Value

array{attached: array, detached: array, updated: array}

array{attached: array, detached: array, updated: array} sync(Collection|Model|array $ids, bool $detaching = true)

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

Parameters

Collection|Model|array $ids
bool $detaching

Return Value

array{attached: array, detached: array, updated: array}

array{attached: array, detached: array, updated: array} syncWithPivotValues(Collection|Model|array $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 $ids
array $values
bool $detaching

Return Value

array{attached: array, detached: array, updated: array}

protected array formatRecordsList(array $records)

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

Parameters

array $records

Return Value

array

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

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

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

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

Attach a model to the parent.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

void

protected void attachUsingCustomClass(mixed $id, array $attributes)

Attach a model to the parent using a custom class.

Parameters

mixed $id
array $attributes

Return Value

void

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

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

Create a full attachment record payload.

Parameters

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

Return Value

array

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

protected array baseAttachRecord(int $id, bool $timed)

Create a new pivot attachment record.

Parameters

int $id
bool $timed

Return Value

array

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

bool hasPivotColumn(string $column)

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

Parameters

string $column

Return Value

bool

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

Detach models from the relationship.

Parameters

mixed $ids
bool $touch

Return Value

int

protected int detachUsingCustomClass(mixed $ids)

Detach models from the relationship using a custom class.

Parameters

mixed $ids

Return Value

int

protected Collection getCurrentlyAttachedPivots()

Get the pivot models that are currently attached.

Return Value

Collection

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

Create a new pivot model instance.

Parameters

array $attributes
bool $exists

Return Value

Pivot

Pivot newExistingPivot(array $attributes = [])

Create a new existing pivot model instance.

Parameters

array $attributes

Return Value

Pivot

Builder newPivotStatement()

Get a new plain query builder for the pivot table.

Return Value

Builder

Builder newPivotStatementForId(mixed $id)

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

Parameters

mixed $id

Return Value

Builder

Builder newPivotQuery()

Create a new query builder for the pivot table.

Return Value

Builder

$this withPivot(array|mixed $columns)

Set the columns on the pivot table to retrieve.

Parameters

array|mixed $columns

Return Value

$this

protected array parseIds(mixed $value)

Get all of the IDs from the given mixed value.

Parameters

mixed $value

Return Value

array

protected mixed parseId(mixed $value)

Get the ID from the given mixed value.

Parameters

mixed $value

Return Value

mixed

protected array castKeys(array $keys)

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

Parameters

array $keys

Return Value

array

protected mixed castKey(mixed $key)

Cast the given key to convert to primary key type.

Parameters

mixed $key

Return Value

mixed

protected array castAttributes(array $attributes)

Cast the given pivot attributes.

Parameters

array $attributes

Return Value

array

protected mixed getTypeSwapValue(string $type, mixed $value)

Converts a given value to a given type value.

Parameters

string $type
mixed $value

Return Value

mixed

Laravel Cloud is the best place to deploy Laravel, Nuxt, Express, Hono, Node.js, Bun, Go, Flask, Python, and more, with dedicated infrastructure available on AWS through Laravel Private Cloud.