trait HasRelationships

Properties

protected array $relations

The loaded relationships for the model.

protected array $touches

The relationships that should be touched on save.

protected Closure|null $relationAutoloadCallback

The relationship autoloader callback.

protected mixed $relationAutoloadContext

The relationship autoloader callback context.

static string[] $manyMethods

The many to many relationship methods.

static protected array $relationResolvers

The relation resolver callbacks.

Methods

void
initializeHasRelationships()

Initialize the HasRelationships trait.

Closure|null
relationResolver(string $class, string $key)

Get the dynamic relation resolver if defined or inherited, or return null.

static void
resolveRelationUsing(string $name, Closure $callback)

Define a dynamic relation resolver.

bool
hasRelationAutoloadCallback()

Determine if a relationship autoloader callback has been defined.

autoloadRelationsUsing(Closure $callback, mixed $context = null)

Define an automatic relationship autoloader callback for this model and its relations.

bool
attemptToAutoloadRelation(string $key)

Attempt to autoload the given relationship using the autoload callback.

void
invokeRelationAutoloadCallbackFor(string $key, array $tuples)

Invoke the relationship autoloader callback for the given relationships.

void
propagateRelationAutoloadCallbackToRelation(string $key, mixed $models)

Propagate the relationship autoloader callback to the given related models.

hasOne(string $related, string|null $foreignKey = null, string|null $localKey = null)

Define a one-to-one relationship.

newHasOne(Builder $query, Model $parent, string $foreignKey, string $localKey)

Instantiate a new HasOne relationship.

hasOneThrough(string $related, string $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-one-through relationship.

newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)

Instantiate a new HasOneThrough relationship.

morphOne(string $related, string $name, string|null $type = null, string|null $id = null, string|null $localKey = null)

Define a polymorphic one-to-one relationship.

newMorphOne(Builder $query, Model $parent, string $type, string $id, string $localKey)

Instantiate a new MorphOne relationship.

belongsTo(string $related, string|null $foreignKey = null, string|null $ownerKey = null, string|null $relation = null)

Define an inverse one-to-one or many relationship.

newBelongsTo(Builder $query, Model $child, string $foreignKey, string $ownerKey, string $relation)

Instantiate a new BelongsTo relationship.

morphTo(string|null $name = null, string|null $type = null, string|null $id = null, string|null $ownerKey = null)

Define a polymorphic, inverse one-to-one or many relationship.

morphEagerTo(string $name, string $type, string $id, string|null $ownerKey)

Define a polymorphic, inverse one-to-one or many relationship.

morphInstanceTo(string|int $target, string $name, string $type, string $id, string|null $ownerKey)

Define a polymorphic, inverse one-to-one or many relationship.

newMorphTo(Builder $query, Model $parent, string $foreignKey, string|null $ownerKey, string $type, string $relation)

Instantiate a new MorphTo relationship.

static string
getActualClassNameForMorph(string|int $class)

Retrieve the actual class name for a given morph class.

string
guessBelongsToRelation()

Guess the "belongs to" relationship name.

through(string|HasOneOrMany $relationship)

Create a pending has-many-through or has-one-through relationship.

hasMany(string $related, string|null $foreignKey = null, string|null $localKey = null)

Define a one-to-many relationship.

newHasMany(Builder $query, Model $parent, string $foreignKey, string $localKey)

Instantiate a new HasMany relationship.

hasManyThrough(string $related, string $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-many-through relationship.

newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)

Instantiate a new HasManyThrough relationship.

morphMany(string $related, string $name, string|null $type = null, string|null $id = null, string|null $localKey = null)

Define a polymorphic one-to-many relationship.

newMorphMany(Builder $query, Model $parent, string $type, string $id, string $localKey)

Instantiate a new MorphMany relationship.

belongsToMany(string $related, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null)

Define a many-to-many relationship.

newBelongsToMany(Builder $query, Model $parent, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string|null $relationName = null)

Instantiate a new BelongsToMany relationship.

morphToMany(string $related, string $name, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null, bool $inverse = false)

Define a polymorphic many-to-many relationship.

newMorphToMany(Builder $query, Model $parent, string $name, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string|null $relationName = null, bool $inverse = false)

Instantiate a new MorphToMany relationship.

morphedByMany(string $related, string $name, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null)

Define a polymorphic, inverse many-to-many relationship.

string|null
guessBelongsToManyRelation()

Get the relationship name of the belongsToMany relationship.

string
joiningTable(string $related, Model|null $instance = null)

Get the joining table name for a many-to-many relation.

string
joiningTableSegment()

Get this model's half of the intermediate table name for belongsToMany relationships.

bool
touches(string $relation)

Determine if the model touches a given relation.

void
touchOwners()

Touch the owning relations of the model.

array
getMorphs(string $name, string|null $type, string|null $id)

Get the polymorphic relationship columns.

string
getMorphClass()

Get the class name for polymorphic relations.

newRelatedInstance(string $class)

Create a new model instance for a related model.

newRelatedThroughInstance(string $class)

Create a new model instance for a related "through" model.

array
getRelations()

Get all the loaded relations for the instance.

mixed
getRelation(string $relation)

Get a specified relationship.

bool
relationLoaded(string $key)

Determine if the given relation is loaded.

setRelation(string $relation, mixed $value)

Set the given relationship on the model.

unsetRelation(string $relation)

Unset a loaded relationship.

setRelations(array $relations)

Set the entire relations array on the model.

withRelationshipAutoloading()

Enable relationship autoloading for this model.

withoutRelations()

Duplicate the instance and unset all the loaded relations.

withoutRelation(array|string $relations)

Duplicate the instance and unset the given loaded relations.

unsetRelations()

Unset all the loaded relations for the instance.

array
getTouchedRelations()

Get the relationships that are touched on save.

setTouchedRelations(array $touches)

Set the relationships that are touched on save.

Details

at line 70
void initializeHasRelationships()

Initialize the HasRelationships trait.

Return Value

void

at line 89
Closure|null relationResolver(string $class, string $key)

Get the dynamic relation resolver if defined or inherited, or return null.

Parameters

string $class
string $key

Return Value

Closure|null

at line 109
static void resolveRelationUsing(string $name, Closure $callback)

Define a dynamic relation resolver.

Boot-only. The resolver persists in a static property for the worker lifetime and runs on every relation resolution for this model class across all coroutines.

Parameters

string $name
Closure $callback

Return Value

void

at line 120
bool hasRelationAutoloadCallback()

Determine if a relationship autoloader callback has been defined.

Return Value

bool

at line 128
HasRelationships autoloadRelationsUsing(Closure $callback, mixed $context = null)

Define an automatic relationship autoloader callback for this model and its relations.

Parameters

Closure $callback
mixed $context

Return Value

HasRelationships

at line 148
protected bool attemptToAutoloadRelation(string $key)

Attempt to autoload the given relationship using the autoload callback.

Parameters

string $key

Return Value

bool

at line 162
protected void invokeRelationAutoloadCallbackFor(string $key, array $tuples)

Invoke the relationship autoloader callback for the given relationships.

Parameters

string $key
array $tuples

Return Value

void

at line 172
protected void propagateRelationAutoloadCallbackToRelation(string $key, mixed $models)

Propagate the relationship autoloader callback to the given related models.

Parameters

string $key
mixed $models

Return Value

void

at line 201
HasOne hasOne(string $related, string|null $foreignKey = null, string|null $localKey = null)

Define a one-to-one relationship.

Parameters

string $related
string|null $foreignKey
string|null $localKey

Return Value

HasOne

at line 222
protected HasOne newHasOne(Builder $query, Model $parent, string $foreignKey, string $localKey)

Instantiate a new HasOne relationship.

Parameters

Builder $query
Model $parent
string $foreignKey
string $localKey

Return Value

HasOne

at line 237
HasOneThrough hasOneThrough(string $related, string $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-one-through relationship.

Parameters

string $related
string $through
string|null $firstKey
string|null $secondKey
string|null $localKey
string|null $secondLocalKey

Return Value

HasOneThrough

at line 268
protected HasOneThrough newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)

Instantiate a new HasOneThrough relationship.

Parameters

Builder $query
Model $farParent
Model $throughParent
string $firstKey
string $secondKey
string $localKey
string $secondLocalKey

Return Value

HasOneThrough

at line 281
MorphOne morphOne(string $related, string $name, string|null $type = null, string|null $id = null, string|null $localKey = null)

Define a polymorphic one-to-one relationship.

Parameters

string $related
string $name
string|null $type
string|null $id
string|null $localKey

Return Value

MorphOne

at line 302
protected MorphOne newMorphOne(Builder $query, Model $parent, string $type, string $id, string $localKey)

Instantiate a new MorphOne relationship.

Parameters

Builder $query
Model $parent
string $type
string $id
string $localKey

Return Value

MorphOne

at line 315
BelongsTo belongsTo(string $related, string|null $foreignKey = null, string|null $ownerKey = null, string|null $relation = null)

Define an inverse one-to-one or many relationship.

Parameters

string $related
string|null $foreignKey
string|null $ownerKey
string|null $relation

Return Value

BelongsTo

at line 357
protected BelongsTo newBelongsTo(Builder $query, Model $child, string $foreignKey, string $ownerKey, string $relation)

Instantiate a new BelongsTo relationship.

Parameters

Builder $query
Model $child
string $foreignKey
string $ownerKey
string $relation

Return Value

BelongsTo

at line 367
MorphTo morphTo(string|null $name = null, string|null $type = null, string|null $id = null, string|null $ownerKey = null)

Define a polymorphic, inverse one-to-one or many relationship.

Parameters

string|null $name
string|null $type
string|null $id
string|null $ownerKey

Return Value

MorphTo

at line 393
protected MorphTo morphEagerTo(string $name, string $type, string $id, string|null $ownerKey)

Define a polymorphic, inverse one-to-one or many relationship.

Parameters

string $name
string $type
string $id
string|null $ownerKey

Return Value

MorphTo

at line 411
protected MorphTo morphInstanceTo(string|int $target, string $name, string $type, string $id, string|null $ownerKey)

Define a polymorphic, inverse one-to-one or many relationship.

Parameters

string|int $target
string $name
string $type
string $id
string|null $ownerKey

Return Value

MorphTo

at line 437
protected MorphTo newMorphTo(Builder $query, Model $parent, string $foreignKey, string|null $ownerKey, string $type, string $relation)

Instantiate a new MorphTo relationship.

Parameters

Builder $query
Model $parent
string $foreignKey
string|null $ownerKey
string $type
string $relation

Return Value

MorphTo

at line 445
static string getActualClassNameForMorph(string|int $class)

Retrieve the actual class name for a given morph class.

Parameters

string|int $class

Return Value

string

at line 453
protected string guessBelongsToRelation()

Guess the "belongs to" relationship name.

Return Value

string

at line 477
PendingHasThroughRelationship through(string|HasOneOrMany $relationship)

Create a pending has-many-through or has-one-through relationship.

Parameters

string|HasOneOrMany $relationship

Return Value

PendingHasThroughRelationship

at line 495
HasMany hasMany(string $related, string|null $foreignKey = null, string|null $localKey = null)

Define a one-to-many relationship.

Parameters

string $related
string|null $foreignKey
string|null $localKey

Return Value

HasMany

at line 521
protected HasMany newHasMany(Builder $query, Model $parent, string $foreignKey, string $localKey)

Instantiate a new HasMany relationship.

Parameters

Builder $query
Model $parent
string $foreignKey
string $localKey

Return Value

HasMany

at line 536
HasManyThrough hasManyThrough(string $related, string $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-many-through relationship.

Parameters

string $related
string $through
string|null $firstKey
string|null $secondKey
string|null $localKey
string|null $secondLocalKey

Return Value

HasManyThrough

at line 567
protected HasManyThrough newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)

Instantiate a new HasManyThrough relationship.

Parameters

Builder $query
Model $farParent
Model $throughParent
string $firstKey
string $secondKey
string $localKey
string $secondLocalKey

Return Value

HasManyThrough

at line 580
MorphMany morphMany(string $related, string $name, string|null $type = null, string|null $id = null, string|null $localKey = null)

Define a polymorphic one-to-many relationship.

Parameters

string $related
string $name
string|null $type
string|null $id
string|null $localKey

Return Value

MorphMany

at line 604
protected MorphMany newMorphMany(Builder $query, Model $parent, string $type, string $id, string $localKey)

Instantiate a new MorphMany relationship.

Parameters

Builder $query
Model $parent
string $type
string $id
string $localKey

Return Value

MorphMany

at line 620
BelongsToMany belongsToMany(string $related, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null)

Define a many-to-many relationship.

Parameters

string $related
string|null $table
string|null $foreignPivotKey
string|null $relatedPivotKey
string|null $parentKey
string|null $relatedKey
string|null $relation

Return Value

BelongsToMany

at line 679
protected BelongsToMany newBelongsToMany(Builder $query, Model $parent, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string|null $relationName = null)

Instantiate a new BelongsToMany relationship.

Parameters

Builder $query
Model $parent
string $table
string $foreignPivotKey
string $relatedPivotKey
string $parentKey
string $relatedKey
string|null $relationName

Return Value

BelongsToMany

at line 700
MorphToMany morphToMany(string $related, string $name, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null, bool $inverse = false)

Define a polymorphic many-to-many relationship.

Parameters

string $related
string $name
string|null $table
string|null $foreignPivotKey
string|null $relatedPivotKey
string|null $parentKey
string|null $relatedKey
string|null $relation
bool $inverse

Return Value

MorphToMany

at line 757
protected MorphToMany newMorphToMany(Builder $query, Model $parent, string $name, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string|null $relationName = null, bool $inverse = false)

Instantiate a new MorphToMany relationship.

Parameters

Builder $query
Model $parent
string $name
string $table
string $foreignPivotKey
string $relatedPivotKey
string $parentKey
string $relatedKey
string|null $relationName
bool $inverse

Return Value

MorphToMany

at line 791
MorphToMany morphedByMany(string $related, string $name, string|null $table = null, string|null $foreignPivotKey = null, string|null $relatedPivotKey = null, string|null $parentKey = null, string|null $relatedKey = null, string|null $relation = null)

Define a polymorphic, inverse many-to-many relationship.

Parameters

string $related
string $name
string|null $table
string|null $foreignPivotKey
string|null $relatedPivotKey
string|null $parentKey
string|null $relatedKey
string|null $relation

Return Value

MorphToMany

at line 824
protected string|null guessBelongsToManyRelation()

Get the relationship name of the belongsToMany relationship.

Return Value

string|null

at line 840
string joiningTable(string $related, Model|null $instance = null)

Get the joining table name for a many-to-many relation.

Parameters

string $related
Model|null $instance

Return Value

string

at line 863
string joiningTableSegment()

Get this model's half of the intermediate table name for belongsToMany relationships.

Return Value

string

at line 871
bool touches(string $relation)

Determine if the model touches a given relation.

Parameters

string $relation

Return Value

bool

at line 879
void touchOwners()

Touch the owning relations of the model.

Return Value

void

at line 901
protected array getMorphs(string $name, string|null $type, string|null $id)

Get the polymorphic relationship columns.

Parameters

string $name
string|null $type
string|null $id

Return Value

array

at line 911
string getMorphClass()

Get the class name for polymorphic relations.

Return Value

string

Exceptions

ClassMorphViolationException

at line 939
protected Model newRelatedInstance(string $class)

Create a new model instance for a related model.

Parameters

string $class

Return Value

Model

at line 956
protected Model newRelatedThroughInstance(string $class)

Create a new model instance for a related "through" model.

Parameters

string $class

Return Value

Model

at line 964
array getRelations()

Get all the loaded relations for the instance.

Return Value

array

at line 972
mixed getRelation(string $relation)

Get a specified relationship.

Parameters

string $relation

Return Value

mixed

at line 980
bool relationLoaded(string $key)

Determine if the given relation is loaded.

Parameters

string $key

Return Value

bool

at line 990
HasRelationships setRelation(string $relation, mixed $value)

Set the given relationship on the model.

Parameters

string $relation
mixed $value

Return Value

HasRelationships

at line 1004
HasRelationships unsetRelation(string $relation)

Unset a loaded relationship.

Parameters

string $relation

Return Value

HasRelationships

at line 1016
HasRelationships setRelations(array $relations)

Set the entire relations array on the model.

Parameters

array $relations

Return Value

HasRelationships

at line 1028
HasRelationships withRelationshipAutoloading()

Enable relationship autoloading for this model.

Return Value

HasRelationships

at line 1038
HasRelationships withoutRelations()

Duplicate the instance and unset all the loaded relations.

Return Value

HasRelationships

at line 1048
HasRelationships withoutRelation(array|string $relations)

Duplicate the instance and unset the given loaded relations.

Parameters

array|string $relations

Return Value

HasRelationships

at line 1064
HasRelationships unsetRelations()

Unset all the loaded relations for the instance.

Return Value

HasRelationships

at line 1074
array getTouchedRelations()

Get the relationships that are touched on save.

Return Value

array

at line 1084
HasRelationships setTouchedRelations(array $touches)

Set the relationships that are touched on save.

Parameters

array $touches

Return Value

HasRelationships