trait HasRelationships (View source)

Properties

protected array $relations

The loaded relationships for the model.

protected array $touches

The relationships that should be touched on save.

static string[] $manyMethods

The many to many relationship methods.

static protected array $relationResolvers

The relation resolver callbacks.

Methods

Closure|null
relationResolver(TRelatedModel> $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.

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

Define a one-to-one relationship.

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

Instantiate a new HasOne relationship.

TIntermediateModel, $this>
hasOneThrough(TRelatedModel> $related, TIntermediateModel> $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-one-through relationship.

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

Instantiate a new HasOneThrough relationship.

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

Define a polymorphic one-to-one relationship.

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

Instantiate a new MorphOne relationship.

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

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

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

Instantiate a new BelongsTo relationship.

Model, $this>
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.

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

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

Model, $this>
morphInstanceTo(string $target, string $name, string $type, string $id, string|null $ownerKey)

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

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

Instantiate a new MorphTo relationship.

static string
getActualClassNameForMorph(string $class)

Retrieve the actual class name for a given morph class.

string
guessBelongsToRelation()

Guess the "belongs to" relationship name.

TIntermediateModel, $this>>))
through(string|TIntermediateModel, $this>|TIntermediateModel, $this> $relationship)

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

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

Define a one-to-many relationship.

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

Instantiate a new HasMany relationship.

TIntermediateModel, $this>
hasManyThrough(TRelatedModel> $related, TIntermediateModel> $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-many-through relationship.

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

Instantiate a new HasManyThrough relationship.

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

Define a polymorphic one-to-many relationship.

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

Instantiate a new MorphMany relationship.

TRelatedModel, $this>
belongsToMany(TRelatedModel> $related, string|Model>|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.

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

Instantiate a new BelongsToMany relationship.

TRelatedModel, $this>
morphToMany(TRelatedModel> $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.

TDeclaringModel>
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.

TRelatedModel, $this>
morphedByMany(TRelatedModel> $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 $type, string $id)

Get the polymorphic relationship columns.

string
getMorphClass()

Get the class name for polymorphic relations.

TRelatedModel
newRelatedInstance(TRelatedModel> $class)

Create a new model instance for a related model.

TRelatedModel
newRelatedThroughInstance(TRelatedModel> $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.

$this
setRelation(string $relation, mixed $value)

Set the given relationship on the model.

$this
unsetRelation(string $relation)

Unset a loaded relationship.

$this
setRelations(array $relations)

Set the entire relations array on the model.

$this
withoutRelations()

Duplicate the instance and unset all the loaded relations.

$this
unsetRelations()

Unset all the loaded relations for the instance.

array
getTouchedRelations()

Get the relationships that are touched on save.

$this
setTouchedRelations(array $touches)

Set the relationships that are touched on save.

Details

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

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

Parameters

TRelatedModel> $class
string $key

Return Value

Closure|null

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

Define a dynamic relation resolver.

Parameters

string $name
Closure $callback

Return Value

void

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

Define a one-to-one relationship.

Parameters

TRelatedModel> $related
string|null $foreignKey
string|null $localKey

Return Value

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

TIntermediateModel, $this> hasOneThrough(TRelatedModel> $related, TIntermediateModel> $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-one-through relationship.

Parameters

TRelatedModel> $related
TIntermediateModel> $through
string|null $firstKey
string|null $secondKey
string|null $localKey
string|null $secondLocalKey

Return Value

TIntermediateModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

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

Define a polymorphic one-to-one relationship.

Parameters

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

Return Value

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

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

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

Parameters

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

Return Value

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

Model, $this> 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

Model, $this>

protected Model, $this> 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

Model, $this>

protected Model, $this> morphInstanceTo(string $target, string $name, string $type, string $id, string|null $ownerKey)

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

Parameters

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

Return Value

Model, $this>

protected TDeclaringModel> 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

TDeclaringModel>

static string getActualClassNameForMorph(string $class)

Retrieve the actual class name for a given morph class.

Parameters

string $class

Return Value

string

protected string guessBelongsToRelation()

Guess the "belongs to" relationship name.

Return Value

string

TIntermediateModel, $this>>)) through(string|TIntermediateModel, $this>|TIntermediateModel, $this> $relationship)

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

Parameters

string|TIntermediateModel, $this>|TIntermediateModel, $this> $relationship

Return Value

TIntermediateModel, $this>>))

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

Define a one-to-many relationship.

Parameters

TRelatedModel> $related
string|null $foreignKey
string|null $localKey

Return Value

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

TIntermediateModel, $this> hasManyThrough(TRelatedModel> $related, TIntermediateModel> $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null, string|null $secondLocalKey = null)

Define a has-many-through relationship.

Parameters

TRelatedModel> $related
TIntermediateModel> $through
string|null $firstKey
string|null $secondKey
string|null $localKey
string|null $secondLocalKey

Return Value

TIntermediateModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

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

Define a polymorphic one-to-many relationship.

Parameters

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

Return Value

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

TRelatedModel, $this> belongsToMany(TRelatedModel> $related, string|Model>|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

TRelatedModel> $related
string|Model>|null $table
string|null $foreignPivotKey
string|null $relatedPivotKey
string|null $parentKey
string|null $relatedKey
string|null $relation

Return Value

TRelatedModel, $this>

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

Instantiate a new BelongsToMany relationship.

Parameters

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

Return Value

TDeclaringModel>

TRelatedModel, $this> morphToMany(TRelatedModel> $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

TRelatedModel> $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

TRelatedModel, $this>

protected TDeclaringModel> 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

TDeclaringModel>

TRelatedModel, $this> morphedByMany(TRelatedModel> $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

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

Return Value

TRelatedModel, $this>

protected string|null guessBelongsToManyRelation()

Get the relationship name of the belongsToMany relationship.

Return Value

string|null

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

string joiningTableSegment()

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

Return Value

string

bool touches(string $relation)

Determine if the model touches a given relation.

Parameters

string $relation

Return Value

bool

void touchOwners()

Touch the owning relations of the model.

Return Value

void

protected array getMorphs(string $name, string $type, string $id)

Get the polymorphic relationship columns.

Parameters

string $name
string $type
string $id

Return Value

array

string getMorphClass()

Get the class name for polymorphic relations.

Return Value

string

protected TRelatedModel newRelatedInstance(TRelatedModel> $class)

Create a new model instance for a related model.

Parameters

TRelatedModel> $class

Return Value

TRelatedModel

protected TRelatedModel newRelatedThroughInstance(TRelatedModel> $class)

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

Parameters

TRelatedModel> $class

Return Value

TRelatedModel

array getRelations()

Get all the loaded relations for the instance.

Return Value

array

mixed getRelation(string $relation)

Get a specified relationship.

Parameters

string $relation

Return Value

mixed

bool relationLoaded(string $key)

Determine if the given relation is loaded.

Parameters

string $key

Return Value

bool

$this setRelation(string $relation, mixed $value)

Set the given relationship on the model.

Parameters

string $relation
mixed $value

Return Value

$this

$this unsetRelation(string $relation)

Unset a loaded relationship.

Parameters

string $relation

Return Value

$this

$this setRelations(array $relations)

Set the entire relations array on the model.

Parameters

array $relations

Return Value

$this

$this withoutRelations()

Duplicate the instance and unset all the loaded relations.

Return Value

$this

$this unsetRelations()

Unset all the loaded relations for the instance.

Return Value

$this

array getTouchedRelations()

Get the relationships that are touched on save.

Return Value

array

$this setTouchedRelations(array $touches)

Set the relationships that are touched on save.

Parameters

array $touches

Return Value

$this

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.