BelongsToMany
class BelongsToMany extends Relation
Traits
Constants
| protected CONSTRAINTS_CONTEXT_KEY |
The context key for storing whether constraints are enabled. |
| protected NESTED_CONSTRAINTS_CONTEXT_KEY |
The context key for enabling constraints on nested relation attributes. |
Properties
| static protected array | $macros | The registered string macros. |
from Macroable |
| protected TRelatedModel> | $query | The Eloquent query builder instance. |
from Relation |
| protected TDeclaringModel | $parent | The parent model instance. |
from Relation |
| protected TRelatedModel | $related | The related model instance. |
from Relation |
| protected bool | $eagerKeysWereEmpty | Indicates whether the eagerly loaded relation should implicitly return an empty collection. |
from Relation |
| static array<int|Model>> | $morphMap | An array to map morph names to their class names in the database. |
from Relation |
| static protected bool | $requireMorphMap | Prevents morph relationships without a morph map. |
from Relation |
| static protected int | $selfJoinCount | The count of self joins. |
from Relation |
| protected string|null | $declaringInverseRelationship | The name of the declaring model's relationship on the pivot. |
from SupportsPivotInverseRelations |
| protected string|null | $relatedInverseRelationship | The name of the related model's relationship on the pivot. |
from SupportsPivotInverseRelations |
| protected string | $table | The intermediate table for the relation. |
|
| protected string | $foreignPivotKey | The foreign key of the parent model. |
|
| protected string | $relatedPivotKey | The associated key of the relation. |
|
| protected string | $parentKey | The key name of the parent model. |
|
| protected string | $relatedKey | The key name of the related model. |
|
| protected string|null | $relationName | The "name" of the relationship. |
|
| protected string[] | $pivotColumns | The pivot table columns to retrieve. |
|
| protected list<array{string, array<int, mixed>}> | $pivotConstraints | The pivot table restrictions in their original order. |
|
| protected Expression|string, value: mixed}> | $pivotValues | The default values for the pivot columns. |
|
| bool | $withTimestamps | Indicates if timestamps are available on the pivot table. |
|
| protected string|null | $pivotCreatedAt | The custom pivot table column for the created_at timestamp. |
|
| protected string|null | $pivotUpdatedAt | The custom pivot table column for the updated_at timestamp. |
|
| protected null|TPivotModel> | $using | The class name of the custom pivot model to use for the relationship. |
|
| protected TAccessor | $accessor | The name of the accessor to use for the "pivot" relationship. |
Methods
Forward a method call to the given object.
Forward a method call to the given object, returning $this if the forwarded call returned itself.
Throw a bad method call exception for the given method.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Create a new belongs to many relationship instance.
Run a callback without constraints while preserving them for nested relation attributes.
Run a callback with the configured relation constraints.
Run a callback with constraints when resolving a nested relation attribute.
Determine if constraints should be added to the relation query.
Set the base constraints on the relation query.
Set the constraints for an eager load of the relation.
Initialize the relation on a set of models.
Match the eagerly loaded results to their parents.
Get the results of the relationship.
Execute the query and get the first result if it's the sole matching record.
Execute the query as a "select" statement.
Touch all of the related models for the relationship.
Add the constraints for a relationship count query.
Add the constraints for an internal relationship existence query.
Get a relationship join table hash.
Get all of the primary keys for an array of models.
Get the query builder that will contain the relationship constraints.
Get the fully-qualified parent key name for the relation.
Get the name of the "created at" column.
Get the name of the "updated at" column.
Add a whereIn eager constraint for the given set of model keys to be loaded.
Prevent polymorphic relationships from being used without model mappings.
Determine if polymorphic relationships require explicit model mapping.
Define the morph map for polymorphic relations and require all morphed models to be explicitly mapped.
Set or get the morph map for polymorphic relations.
Build a table-keyed array from model class names.
Get the model associated with a custom polymorphic type.
Get the alias associated with a custom polymorphic class.
Get a dictionary key attribute - casting it to a string if necessary.
Toggles a model (or models) from the parent.
Toggle a model (or models) from the parent within a transaction.
Sync the intermediate tables with a list of IDs without detaching.
Sync the intermediate tables with a list of IDs or collection of models.
Sync the intermediate tables with a list of IDs or collection of models within a transaction.
Sync the intermediate tables with a list of IDs without detaching within a transaction.
Sync the intermediate tables with a list of IDs or collection of models with the given pivot values.
Sync the intermediate tables with a list of IDs or collection of models with the given pivot values within a transaction.
Format the sync / toggle record list so that it is keyed by ID.
Attach all of the records that aren't in the given current records.
Update an existing pivot record on the table.
Update an existing pivot record on the table within a transaction.
Update an existing pivot record on the table via a custom class.
Attach a model to the parent.
Attach a model to the parent within a transaction.
Attach a model to the parent using a custom class.
Create an array of records to insert into the pivot table.
Create a full attachment record payload.
Get the attach record ID and extra attributes.
Create a new pivot attachment record.
Set the creation and update timestamps on an attach record.
Determine whether the given column is defined as a pivot column.
Detach models from the relationship.
Detach models from the relationship within a transaction.
Detach models from the relationship using a custom class.
Get the pivot models that are currently attached.
Get the pivot models that are currently attached, filtered by related model keys.
Create a new pivot model instance.
Create a new existing pivot model instance.
Get a new plain query builder for the pivot table.
Get the connection that owns the pivot table.
Get a new pivot statement for a given "other" ID.
Create a new query builder for the pivot table.
Determine whether the relation has pivot constraints.
Set the columns on the pivot table to retrieve.
Get all of the IDs from the given mixed value.
Cast the given keys to integers if they are numeric and string otherwise.
Cast the given key to convert to primary key type.
Cast the given pivot attributes.
Converts a given value to a given type value.
Instruct Eloquent to link the declaring and related models back to the pivot after the relationship query has run.
Remove the chaperone relationships for this query.
Resolve the inverse relation name on the pivot for a given model.
Attempt to guess the inverse relation name on the pivot for a given model.
Derive the inverse relationship name from a pivot key.
Apply chaperone relationships to a pivot model instance.
Attempt to resolve the intermediate table name from the given string.
Set the where clause for the relation query.
Get the class being used for pivot models.
Specify the custom pivot model to use for the relationship.
Specify the custom pivot accessor to use for the relationship.
Set a where clause for a pivot table column.
Apply and record a pivot restriction for subsequent pivot writes.
Set a "where between" clause for a pivot table column.
Set a "or where between" clause for a pivot table column.
Set a "where pivot not between" clause for a pivot table column.
Set a "or where not between" clause for a pivot table column.
Set a "where in" clause for a pivot table column.
Set an "or where" clause for a pivot table column.
Set a where clause for a pivot table column.
Set an "or where in" clause for a pivot table column.
Set a "where not in" clause for a pivot table column.
Set an "or where not in" clause for a pivot table column.
Set a "where null" clause for a pivot table column.
Set a "where not null" clause for a pivot table column.
Set a "or where null" clause for a pivot table column.
Set a "or where not null" clause for a pivot table column.
Add an "order by" clause for a pivot table column.
Find a related model by its primary key or return a new instance of the related model.
Get the first related model record matching the attributes or instantiate it.
Get the first record matching the attributes. If the record is not found, create it.
Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record.
Determine if the related model is attached through the current pivot constraints.
Create or update a related record matching the attributes, and fill it with values.
Find a related model by its primary key.
Find a related model by its primary key or throw an exception.
Normalize primary keys for related-model lookups.
Add a basic where clause to the query, and return the first result.
Execute the query and get the first result or throw an exception.
Get the select columns for the relation query.
Get the pivot columns for the relation.
Get a paginator for the "select" statement.
Paginate the given query into a simple paginator.
Paginate the given query into a cursor paginator.
Chunk the results of the query.
Chunk the results of a query by comparing numeric IDs.
Chunk the results of a query by comparing IDs in descending order.
Execute a callback over each item while chunking by ID.
Chunk the results of a query by comparing IDs in a given order.
Execute a callback over each item while chunking.
Query lazily, by chunks of the given size.
Query lazily, by chunking the results of a query by comparing IDs.
Query lazily, by chunking the results of a query by comparing IDs in descending order.
Get a lazy collection for the given query.
Prepare the query builder for query execution.
Hydrate the pivot table relationship on the models.
If we're touching the parent model, touch.
Determine if we should touch the parent on sync.
Attempt to guess the name of the inverse of the relation.
Get all of the IDs for the related models.
Save a new model without raising any events and attach it to the parent model.
Save an array of new models and attach them to the parent model.
Save an array of new models without raising any events and attach them to the parent model.
Create a new instance of the related model.
Create an array of new instances of the related models.
Add the constraints for a relationship query on the same table.
Alias to set the "limit" value of the query.
Set the "limit" value of the query.
Get the key for comparing against the parent key in "has" query.
Specify that the pivot table has creation and update timestamps.
Get the foreign key for the relation.
Get the fully-qualified foreign key for the relation.
Get the "related key" for the relation.
Get the fully-qualified "related key" for the relation.
Get the parent key for the relationship.
Get the related key for the relationship.
Get the fully-qualified related key name for the relation.
Get the intermediate table for the relationship.
Get the relationship name for the relationship.
Get the name of the pivot accessor for this relationship.
Get the pivot columns for this relationship.
Details
in
ForwardsCalls at line 22
protected mixed
forwardCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object.
in
ForwardsCalls at line 52
protected mixed
forwardDecoratedCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object, returning $this if the forwarded call returned itself.
in
ForwardsCalls at line 66
static protected never
throwBadMethodCallException(string $method)
Throw a bad method call exception for the given method.
in
Macroable at line 28
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 41
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 68
static void
flushMacros()
Flush the existing macros.
Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 103
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
at line 137
__construct(Builder $query, Model $parent, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string|null $relationName = null)
Create a new belongs to many relationship instance.
in
Relation at line 110
static mixed
noConstraints(Closure $callback)
Run a callback with constraints disabled on the relation.
in
Relation at line 123
static mixed
noConstraintsForRelation(Closure $callback)
Run a callback without constraints while preserving them for nested relation attributes.
in
Relation at line 136
static protected mixed
withoutConstraints(Closure $callback, bool $constraintsForNestedRelations)
Run a callback with the configured relation constraints.
in
Relation at line 163
static mixed
withConstraints(Closure $callback)
Run a callback with constraints enabled on the relation.
in
Relation at line 184
static mixed
withConstraintsForNestedRelation(Closure $callback)
Run a callback with constraints when resolving a nested relation attribute.
in
Relation at line 194
static bool
shouldAddConstraints()
Determine if constraints should be added to the relation query.
at line 182
void
addConstraints()
Set the base constraints on the relation query.
at line 230
void
addEagerConstraints(array $models)
Set the constraints for an eager load of the relation.
at line 241
array
initRelation(array $models, string $relation)
Initialize the relation on a set of models.
at line 250
array
match(array $models, Collection $results, string $relation)
Match the eagerly loaded results to their parents.
at line 941
TResult
getResults()
Get the results of the relationship.
in
Relation at line 240
Collection
getEager()
Get the relationship for eager loading.
in
Relation at line 255
Model
sole(array|string $columns = ['*'])
Execute the query and get the first result if it's the sole matching record.
at line 953
Collection
get(array $columns = ['*'])
Execute the query as a "select" statement.
at line 1285
void
touch()
Touch all of the related models for the relationship.
E.g.: Touch all roles associated with this user.
in
Relation at line 299
int
rawUpdate(array $attributes = [])
Run a raw update against the base query.
in
Relation at line 311
Builder
getRelationExistenceCountQuery(Builder $query, Builder $parentQuery)
Add the constraints for a relationship count query.
at line 1414
Builder
getRelationExistenceQuery(Builder $query, Builder $parentQuery, mixed $columns = ['*'])
Add the constraints for an internal relationship existence query.
Essentially, these queries compare on column names like whereColumn.
in
Relation at line 343
string
getRelationCountHash(bool $incrementJoinCount = true)
Get a relationship join table hash.
in
Relation at line 354
protected array
getKeys(array $models, string|null $key = null)
Get all of the primary keys for an array of models.
in
Relation at line 366
protected Builder
getRelationQuery()
Get the query builder that will contain the relationship constraints.
in
Relation at line 384
Builder
getBaseQuery()
Get the base query builder driving the Eloquent builder.
at line 1566
string
getQualifiedParentKeyName()
Get the fully-qualified parent key name for the relation.
in
Relation at line 430
string
getRelatedClass()
Get the class name of the related model.
at line 1510
string
createdAt()
Get the name of the "created at" column.
at line 1518
string
updatedAt()
Get the name of the "updated at" column.
in
Relation at line 454
string
relatedUpdatedAt()
Get the name of the related model's "updated at" column.
in
Relation at line 464
protected void
whereInEager(string $whereIn, string $key, array $modelKeys, Builder|null $query = null)
Add a whereIn eager constraint for the given set of model keys to be loaded.
in
Relation at line 476
protected string
whereInMethod(Model $model, string $key)
Get the name of the "where in" method for eager loading.
in
Relation at line 490
static void
requireMorphMap(bool $requireMorphMap = true)
Prevent polymorphic relationships from being used without model mappings.
Boot-only. The flag persists in a static property for the worker lifetime and applies to every polymorphic resolution across all coroutines.
in
Relation at line 498
static bool
requiresMorphMap()
Determine if polymorphic relationships require explicit model mapping.
in
Relation at line 512
static array
enforceMorphMap(array $map, bool $merge = true)
Define the morph map for polymorphic relations and require all morphed models to be explicitly mapped.
Boot-only. Sets both worker-wide morph state (requireMorphMap + morphMap) shared by every coroutine.
in
Relation at line 529
static array
morphMap(array|null $map = null, bool $merge = true)
Set or get the morph map for polymorphic relations.
Boot-only when setting. The map persists in a static property for the worker lifetime and applies to every polymorphic resolution across all coroutines.
in
Relation at line 548
static protected array|null
buildMorphMapFromModels(array|null $models = null)
Build a table-keyed array from model class names.
in
Relation at line 564
static string|null
getMorphedModel(int|string|null $alias)
Get the model associated with a custom polymorphic type.
in
Relation at line 578
static int|string
getMorphAlias(string $className)
Get the alias associated with a custom polymorphic class.
in
Relation at line 588
static void
flushState()
Flush all static state.
in
Relation at line 611
void
__clone()
Force a clone of the underlying query builder when cloning.
in
InteractsWithDictionary at line 19
protected int|string|null
getDictionaryKey(mixed $attribute)
Get a dictionary key attribute - casting it to a string if necessary.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable at line 73
array
toggleOrFail(mixed $ids, bool $touch = true)
Toggle a model (or models) from the parent within a transaction.
in
InteractsWithPivotTable at line 83
array
syncWithoutDetaching(Collection|Model|array|int|string|null $ids)
Sync the intermediate tables with a list of IDs without detaching.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable at line 196
protected array
formatRecordsList(array $records)
Format the sync / toggle record list so that it is keyed by ID.
in
InteractsWithPivotTable 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.
in
InteractsWithPivotTable at line 241
int
updateExistingPivot(mixed $id, array $attributes, bool $touch = true)
Update an existing pivot record on the table.
in
InteractsWithPivotTable at line 267
int
updateExistingPivotOrFail(mixed $id, array $attributes, bool $touch = true)
Update an existing pivot record on the table within a transaction.
in
InteractsWithPivotTable at line 275
protected int
updateExistingPivotUsingCustomClass(mixed $id, array $attributes, bool $touch)
Update an existing pivot record on the table via a custom class.
in
InteractsWithPivotTable at line 295
void
attach(mixed $ids, array $attributes = [], bool $touch = true)
Attach a model to the parent.
in
InteractsWithPivotTable at line 319
void
attachOrFail(mixed $ids, array $attributes = [], bool $touch = true)
Attach a model to the parent within a transaction.
in
InteractsWithPivotTable at line 327
protected void
attachUsingCustomClass(mixed $ids, array $attributes)
Attach a model to the parent using a custom class.
in
InteractsWithPivotTable at line 342
protected array
formatAttachRecords(array $ids, array $attributes)
Create an array of records to insert into the pivot table.
in
InteractsWithPivotTable at line 367
protected array
formatAttachRecord(int|string $key, mixed $value, array $attributes, bool $hasTimestamps)
Create a full attachment record payload.
in
InteractsWithPivotTable at line 380
protected array
extractAttachIdAndAttributes(mixed $key, mixed $value, array $attributes)
Get the attach record ID and extra attributes.
in
InteractsWithPivotTable at line 390
protected array
baseAttachRecord(mixed $id, bool $timed)
Create a new pivot attachment record.
in
InteractsWithPivotTable at line 413
protected array
addTimestampsToAttachment(array $record, bool $exists = false)
Set the creation and update timestamps on an attach record.
in
InteractsWithPivotTable at line 437
bool
hasPivotColumn(string|null $column)
Determine whether the given column is defined as a pivot column.
in
InteractsWithPivotTable at line 445
int
detach(mixed $ids = null, bool $touch = true)
Detach models from the relationship.
in
InteractsWithPivotTable at line 483
int
detachOrFail(mixed $ids = null, bool $touch = true)
Detach models from the relationship within a transaction.
in
InteractsWithPivotTable at line 491
protected int
detachUsingCustomClass(mixed $ids)
Detach models from the relationship using a custom class.
in
InteractsWithPivotTable at line 500
protected Collection
getCurrentlyAttachedPivots()
Get the pivot models that are currently attached.
in
InteractsWithPivotTable at line 508
protected Collection
getCurrentlyAttachedPivotsForIds(mixed $ids = null)
Get the pivot models that are currently attached, filtered by related model keys.
in
InteractsWithPivotTable at line 522
Model
newPivot(array $attributes = [], bool $exists = false)
Create a new pivot model instance.
in
InteractsWithPivotTable at line 559
Model
newExistingPivot(array $attributes = [])
Create a new existing pivot model instance.
in
InteractsWithPivotTable at line 567
Builder
newPivotStatement()
Get a new plain query builder for the pivot table.
in
InteractsWithPivotTable at line 575
protected ConnectionInterface
getPivotConnection()
Get the connection that owns the pivot table.
in
InteractsWithPivotTable at line 583
Builder
newPivotStatementForId(mixed $id)
Get a new pivot statement for a given "other" ID.
in
InteractsWithPivotTable at line 591
Builder
newPivotQuery()
Create a new query builder for the pivot table.
in
InteractsWithPivotTable at line 609
protected bool
hasPivotConstraints()
Determine whether the relation has pivot constraints.
in
InteractsWithPivotTable at line 619
InteractsWithPivotTable
withPivot(mixed $columns)
Set the columns on the pivot table to retrieve.
in
InteractsWithPivotTable at line 632
protected array
parseIds(mixed $value)
Get all of the IDs from the given mixed value.
in
InteractsWithPivotTable at line 654
protected mixed
parseId(mixed $value)
Get the ID from the given mixed value.
in
InteractsWithPivotTable at line 662
protected array
castKeys(array $keys)
Cast the given keys to integers if they are numeric and string otherwise.
in
InteractsWithPivotTable at line 672
protected mixed
castKey(mixed $key)
Cast the given key to convert to primary key type.
in
InteractsWithPivotTable at line 683
protected array
castAttributes(array $attributes)
Cast the given pivot attributes.
in
InteractsWithPivotTable at line 693
protected mixed
getTypeSwapValue(string $type, mixed $value)
Converts a given value to a given type value.
in
SupportsPivotInverseRelations at line 29
SupportsPivotInverseRelations
chaperone(string|null $declaring = null, string|null $related = null)
Instruct Eloquent to link the declaring and related models back to the pivot after the relationship query has run.
in
SupportsPivotInverseRelations at line 74
SupportsPivotInverseRelations
withoutChaperone()
Remove the chaperone relationships for this query.
in
SupportsPivotInverseRelations at line 90
protected string|null
resolvePivotInverseRelation(Model $pivotModel, string|null $relation, string $foreignKey, Model $model)
Resolve the inverse relation name on the pivot for a given model.
If an explicit name is provided and invalid, an exception is thrown. If guessing fails, null is returned.
in
SupportsPivotInverseRelations at line 106
protected string|null
guessPivotInverseRelation(Model $pivotModel, string $foreignKey, Model $model)
Attempt to guess the inverse relation name on the pivot for a given model.
in
SupportsPivotInverseRelations at line 122
protected string
relationNameFromPivotKey(string $pivotKey, Model $model)
Derive the inverse relationship name from a pivot key.
in
SupportsPivotInverseRelations at line 130
protected void
applyChaperonesToPivot(Model $pivot, Model $declaring, Model $related)
Apply chaperone relationships to a pivot model instance.
at line 160
protected string
resolveTableName(string $table)
Attempt to resolve the intermediate table name from the given string.
at line 197
protected BelongsToMany
performJoin(Builder|null $query = null)
Set the join clause for the relation query.
at line 219
protected BelongsToMany
addWhereConstraints()
Set the where clause for the relation query.
at line 289
protected array
buildDictionary(Collection $results)
Build model dictionary keyed by the relation's foreign key.
at line 320
string
getPivotClass()
Get the class being used for pivot models.
at line 335
BelongsToMany
using(string $class)
Specify the custom pivot model to use for the relationship.
at line 352
BelongsToMany
as(string $accessor)
Specify the custom pivot accessor to use for the relationship.
at line 365
BelongsToMany
wherePivot(Closure|Expression|string $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
Set a where clause for a pivot table column.
at line 390
protected BelongsToMany
addPivotConstraint(string $method, array $arguments)
Apply and record a pivot restriction for subsequent pivot writes.
at line 414
protected BelongsToMany
addCompiledPivotConstraint(Builder $query)
Apply and record an already-built pivot predicate.
at line 438
BelongsToMany
wherePivotBetween(Expression|string $column, array $values, string $boolean = 'and', bool $not = false)
Set a "where between" clause for a pivot table column.
at line 448
BelongsToMany
orWherePivotBetween(Expression|string $column, array $values)
Set a "or where between" clause for a pivot table column.
at line 458
BelongsToMany
wherePivotNotBetween(Expression|string $column, array $values, string $boolean = 'and')
Set a "where pivot not between" clause for a pivot table column.
at line 468
BelongsToMany
orWherePivotNotBetween(Expression|string $column, array $values)
Set a "or where not between" clause for a pivot table column.
at line 478
BelongsToMany
wherePivotIn(Expression|string $column, mixed $values, string $boolean = 'and', bool $not = false)
Set a "where in" clause for a pivot table column.
at line 489
BelongsToMany
orWherePivot(Closure|Expression|string $column, mixed $operator = null, mixed $value = null)
Set an "or where" clause for a pivot table column.
at line 504
BelongsToMany
withPivotValue(array|Expression|string $column, mixed $value = null)
Set a where clause for a pivot table column.
In addition, new pivot records will receive this value.
at line 528
BelongsToMany
orWherePivotIn(Expression|string $column, mixed $values)
Set an "or where in" clause for a pivot table column.
at line 538
BelongsToMany
wherePivotNotIn(Expression|string $column, mixed $values, string $boolean = 'and')
Set a "where not in" clause for a pivot table column.
at line 548
BelongsToMany
orWherePivotNotIn(Expression|string $column, mixed $values)
Set an "or where not in" clause for a pivot table column.
at line 558
BelongsToMany
wherePivotNull(Expression|string $column, string $boolean = 'and', bool $not = false)
Set a "where null" clause for a pivot table column.
at line 568
BelongsToMany
wherePivotNotNull(Expression|string $column, string $boolean = 'and')
Set a "where not null" clause for a pivot table column.
at line 578
BelongsToMany
orWherePivotNull(Expression|string $column, bool $not = false)
Set a "or where null" clause for a pivot table column.
at line 588
BelongsToMany
orWherePivotNotNull(Expression|string $column)
Set a "or where not null" clause for a pivot table column.
at line 599
BelongsToMany
orderByPivot(Expression|string $column, SortDirection|string $direction = SortDirection::Ascending)
Add an "order by" clause for a pivot table column.
at line 609
BelongsToMany
orderByPivotDesc(Expression|string $column)
Add an "order by desc" clause for a pivot table column.
at line 625
Collection|Model
findOrNew(mixed $id, array $columns = ['*'])
Find a related model by its primary key or return a new instance of the related model.
at line 640
Model
firstOrNew(array $attributes = [], Closure|array $values = [])
Get the first related model record matching the attributes or instantiate it.
at line 655
Model
firstOrCreate(array $attributes = [], Closure|array $values = [], array $joining = [], bool $touch = true)
Get the first record matching the attributes. If the record is not found, create it.
at line 684
Model
createOrFirst(array $attributes = [], Closure|array $values = [], array $joining = [], bool $touch = true)
Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record.
at line 710
protected bool
hasAttachedPivot(Model $instance)
Determine if the related model is attached through the current pivot constraints.
at line 723
Model
updateOrCreate(array $attributes, Closure|array $values = [], array $joining = [], bool $touch = true)
Create or update a related record matching the attributes, and fill it with values.
at line 745
Collection&Model&null
find(mixed $id, array $columns = ['*'])
Find a related model by its primary key.
at line 766
Model
findSole(mixed $id, array $columns = ['*'])
Find a sole related model by its primary key.
at line 781
Collection
findMany(Arrayable|array $ids, array $columns = ['*'])
Find multiple related models by their primary keys.
at line 805
Collection&Model
findOrFail(mixed $id, array $columns = ['*'])
Find a related model by its primary key or throw an exception.
at line 837
mixed
findOr(mixed $id, Closure|array|string $columns = ['*'], Closure|null $callback = null)
Find a related model by its primary key or call a callback.
at line 863
protected mixed
parseFindIds(mixed $ids)
Normalize primary keys for related-model lookups.
at line 884
Model|null
firstWhere(Closure|string|array $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
Add a basic where clause to the query, and return the first result.
at line 894
Model|null
first(array $columns = ['*'])
Execute the query and get the first result.
at line 908
Model
firstOrFail(array $columns = ['*'])
Execute the query and get the first result or throw an exception.
at line 926
mixed
firstOr(Closure|array $columns = ['*'], Closure|null $callback = null)
Execute the query and get the first result or call a callback.
at line 983
protected array
shouldSelect(array $columns = ['*'])
Get the select columns for the relation query.
at line 997
protected array
aliasedPivotColumns()
Get the pivot columns for the relation.
"pivot_" is prefixed at each column for easy removal later.
at line 1014
mixed
paginate(int|null $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)
Get a paginator for the "select" statement.
at line 1028
mixed
simplePaginate(int|null $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)
Paginate the given query into a simple paginator.
at line 1042
mixed
cursorPaginate(int|null $perPage = null, array $columns = ['*'], string $cursorName = 'cursor', Cursor|string|null $cursor = null)
Paginate the given query into a cursor paginator.
at line 1058
bool
chunk(int $count, callable $callback)
Chunk the results of the query.
at line 1070
bool
chunkById(int $count, callable $callback, string|null $column = null, string|null $alias = null)
Chunk the results of a query by comparing numeric IDs.
at line 1078
bool
chunkByIdDesc(int $count, callable $callback, string|null $column = null, string|null $alias = null)
Chunk the results of a query by comparing IDs in descending order.
at line 1086
bool
eachById(callable $callback, int $count = 1000, string|null $column = null, string|null $alias = null)
Execute a callback over each item while chunking by ID.
at line 1100
bool
orderedChunkById(int $count, callable $callback, string|null $column = null, string|null $alias = null, SortDirection|bool $descending = false)
Chunk the results of a query by comparing IDs in a given order.
at line 1118
bool
each(callable $callback, int $count = 1000)
Execute a callback over each item while chunking.
at line 1134
mixed
lazy(int $chunkSize = 1000)
Query lazily, by chunks of the given size.
at line 1148
mixed
lazyById(int $chunkSize = 1000, string|null $column = null, string|null $alias = null)
Query lazily, by chunking the results of a query by comparing IDs.
at line 1168
mixed
lazyByIdDesc(int $chunkSize = 1000, string|null $column = null, string|null $alias = null)
Query lazily, by chunking the results of a query by comparing IDs in descending order.
at line 1188
mixed
cursor()
Get a lazy collection for the given query.
at line 1202
protected Builder
prepareQueryBuilder()
Prepare the query builder for query execution.
at line 1213
protected void
hydratePivotRelation(array $models)
Hydrate the pivot table relationship on the models.
at line 1232
protected array
migratePivotAttributes(Model $model)
Get the pivot attributes from a model.
at line 1253
void
touchIfTouching()
If we're touching the parent model, touch.
at line 1267
protected bool
touchingParent()
Determine if we should touch the parent on sync.
at line 1275
protected string
guessInverseRelation()
Attempt to guess the name of the inverse of the relation.
at line 1308
Collection
allRelatedIds()
Get all of the IDs for the related models.
at line 1319
Model
save(Model $model, array $pivotAttributes = [], bool $touch = true)
Save a new model and attach it to the parent model.
at line 1334
Model
saveQuietly(Model $model, array $pivotAttributes = [], bool $touch = true)
Save a new model without raising any events and attach it to the parent model.
at line 1349
iterable
saveMany(iterable $models, array $pivotAttributes = [])
Save an array of new models and attach them to the parent model.
at line 1368
iterable
saveManyQuietly(iterable $models, array $pivotAttributes = [])
Save an array of new models without raising any events and attach them to the parent model.
at line 1380
Model
create(array $attributes = [], array $joining = [], bool $touch = true)
Create a new instance of the related model.
at line 1401
array
createMany(iterable $records, array $joinings = [])
Create an array of new instances of the related models.
at line 1432
Builder
getRelationExistenceQueryForSelfJoin(Builder $query, Builder $parentQuery, mixed $columns = ['*'])
Add the constraints for a relationship query on the same table.
at line 1450
BelongsToMany
take(int $value)
Alias to set the "limit" value of the query.
at line 1460
BelongsToMany
limit(int $value)
Set the "limit" value of the query.
at line 1482
string
getExistenceCompareKey()
Get the key for comparing against the parent key in "has" query.
at line 1492
BelongsToMany
withTimestamps(string|false|null $createdAt = null, string|false|null $updatedAt = null)
Specify that the pivot table has creation and update timestamps.
at line 1526
string
getForeignPivotKeyName()
Get the foreign key for the relation.
at line 1534
string
getQualifiedForeignPivotKeyName()
Get the fully-qualified foreign key for the relation.
at line 1542
string
getRelatedPivotKeyName()
Get the "related key" for the relation.
at line 1550
string
getQualifiedRelatedPivotKeyName()
Get the fully-qualified "related key" for the relation.
at line 1558
string
getParentKeyName()
Get the parent key for the relationship.
at line 1574
string
getRelatedKeyName()
Get the related key for the relationship.
at line 1582
string
getQualifiedRelatedKeyName()
Get the fully-qualified related key name for the relation.
at line 1590
string
getTable()
Get the intermediate table for the relationship.
at line 1598
string|null
getRelationName()
Get the relationship name for the relationship.
at line 1608
string
getPivotAccessor()
Get the name of the pivot accessor for this relationship.
at line 1618
array
getPivotColumns()
Get the pivot columns for this relationship.
at line 1628
Expression|string
qualifyPivotColumn(Expression|string $column)
Qualify the given column name by the pivot table.