HasRelationships
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
Initialize the HasRelationships trait.
Get the dynamic relation resolver if defined or inherited, or return null.
Determine if a relationship autoloader callback has been defined.
Define an automatic relationship autoloader callback for this model and its relations.
Attempt to autoload the given relationship using the autoload callback.
Invoke the relationship autoloader callback for the given relationships.
Propagate the relationship autoloader callback to the given related models.
Define a one-to-one relationship.
Define a has-one-through relationship.
Instantiate a new HasOneThrough relationship.
Define a polymorphic one-to-one relationship.
Instantiate a new MorphOne relationship.
Define an inverse one-to-one or many relationship.
Instantiate a new BelongsTo relationship.
Define a polymorphic, inverse one-to-one or many relationship.
Define a polymorphic, inverse one-to-one or many relationship.
Define a polymorphic, inverse one-to-one or many relationship.
Instantiate a new MorphTo relationship.
Retrieve the actual class name for a given morph class.
Guess the "belongs to" relationship name.
Create a pending has-many-through or has-one-through relationship.
Define a one-to-many relationship.
Instantiate a new HasMany relationship.
Define a has-many-through relationship.
Instantiate a new HasManyThrough relationship.
Define a polymorphic one-to-many relationship.
Instantiate a new MorphMany relationship.
Define a many-to-many relationship.
Instantiate a new BelongsToMany relationship.
Define a polymorphic many-to-many relationship.
Instantiate a new MorphToMany relationship.
Define a polymorphic, inverse many-to-many relationship.
Get the relationship name of the belongsToMany relationship.
Get the joining table name for a many-to-many relation.
Get this model's half of the intermediate table name for belongsToMany relationships.
Determine if the model touches a given relation.
Touch the owning relations of the model.
Get the polymorphic relationship columns.
Get the class name for polymorphic relations.
Create a new model instance for a related model.
Create a new model instance for a related "through" model.
Get all the loaded relations for the instance.
Get a specified relationship.
Determine if the given relation is loaded.
Set the given relationship on the model.
Unset a loaded relationship.
Set the entire relations array on the model.
Enable relationship autoloading for this model.
Duplicate the instance and unset all the loaded relations.
Duplicate the instance and unset the given loaded relations.
Unset all the loaded relations for the instance.
Get the relationships that are touched on save.
Set the relationships that are touched on save.
Details
at line 70
void
initializeHasRelationships()
Initialize the HasRelationships trait.
at line 85
Closure|null
relationResolver(string $class, string $key)
Get the dynamic relation resolver if defined or inherited, or return null.
at line 105
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.
at line 116
bool
hasRelationAutoloadCallback()
Determine if a relationship autoloader callback has been defined.
at line 124
HasRelationships
autoloadRelationsUsing(Closure $callback, mixed $context = null)
Define an automatic relationship autoloader callback for this model and its relations.
at line 144
protected bool
attemptToAutoloadRelation(string $key)
Attempt to autoload the given relationship using the autoload callback.
at line 158
protected void
invokeRelationAutoloadCallbackFor(string $key, array $tuples)
Invoke the relationship autoloader callback for the given relationships.
at line 168
protected void
propagateRelationAutoloadCallbackToRelation(string $key, mixed $models)
Propagate the relationship autoloader callback to the given related models.
at line 197
HasOne
hasOne(string $related, string|null $foreignKey = null, string|null $localKey = null)
Define a one-to-one relationship.
at line 218
protected HasOne
newHasOne(Builder $query, Model $parent, string $foreignKey, string $localKey)
Instantiate a new HasOne relationship.
at line 233
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.
at line 264
protected HasOneThrough
newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)
Instantiate a new HasOneThrough relationship.
at line 277
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.
at line 298
protected MorphOne
newMorphOne(Builder $query, Model $parent, string $type, string $id, string $localKey)
Instantiate a new MorphOne relationship.
at line 311
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.
at line 353
protected BelongsTo
newBelongsTo(Builder $query, Model $child, string $foreignKey, string $ownerKey, string $relation)
Instantiate a new BelongsTo relationship.
at line 363
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.
at line 389
protected MorphTo
morphEagerTo(string $name, string $type, string $id, string|null $ownerKey)
Define a polymorphic, inverse one-to-one or many relationship.
at line 407
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.
at line 433
protected MorphTo
newMorphTo(Builder $query, Model $parent, string $foreignKey, string|null $ownerKey, string $type, string $relation)
Instantiate a new MorphTo relationship.
at line 441
static string
getActualClassNameForMorph(string|int $class)
Retrieve the actual class name for a given morph class.
at line 449
protected string
guessBelongsToRelation()
Guess the "belongs to" relationship name.
at line 473
PendingHasThroughRelationship
through(string|HasOneOrMany $relationship)
Create a pending has-many-through or has-one-through relationship.
at line 491
HasMany
hasMany(string $related, string|null $foreignKey = null, string|null $localKey = null)
Define a one-to-many relationship.
at line 517
protected HasMany
newHasMany(Builder $query, Model $parent, string $foreignKey, string $localKey)
Instantiate a new HasMany relationship.
at line 532
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.
at line 563
protected HasManyThrough
newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, string $firstKey, string $secondKey, string $localKey, string $secondLocalKey)
Instantiate a new HasManyThrough relationship.
at line 576
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.
at line 600
protected MorphMany
newMorphMany(Builder $query, Model $parent, string $type, string $id, string $localKey)
Instantiate a new MorphMany relationship.
at line 614
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.
at line 669
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.
at line 690
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.
at line 747
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.
at line 781
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.
at line 814
protected string|null
guessBelongsToManyRelation()
Get the relationship name of the belongsToMany relationship.
at line 829
string
joiningTable(string $related, Model|null $instance = null)
Get the joining table name for a many-to-many relation.
at line 852
string
joiningTableSegment()
Get this model's half of the intermediate table name for belongsToMany relationships.
at line 860
bool
touches(string $relation)
Determine if the model touches a given relation.
at line 868
void
touchOwners()
Touch the owning relations of the model.
at line 890
protected array
getMorphs(string $name, string|null $type, string|null $id)
Get the polymorphic relationship columns.
at line 898
string
getMorphClass()
Get the class name for polymorphic relations.
at line 925
protected Model
newRelatedInstance(string $class)
Create a new model instance for a related model.
at line 942
protected Model
newRelatedThroughInstance(string $class)
Create a new model instance for a related "through" model.
at line 950
array
getRelations()
Get all the loaded relations for the instance.
at line 958
mixed
getRelation(string $relation)
Get a specified relationship.
at line 966
bool
relationLoaded(string $key)
Determine if the given relation is loaded.
at line 976
HasRelationships
setRelation(string $relation, mixed $value)
Set the given relationship on the model.
at line 990
HasRelationships
unsetRelation(string $relation)
Unset a loaded relationship.
at line 1002
HasRelationships
setRelations(array $relations)
Set the entire relations array on the model.
at line 1014
HasRelationships
withRelationshipAutoloading()
Enable relationship autoloading for this model.
at line 1024
HasRelationships
withoutRelations()
Duplicate the instance and unset all the loaded relations.
at line 1034
HasRelationships
withoutRelation(array|string $relations)
Duplicate the instance and unset the given loaded relations.
at line 1050
HasRelationships
unsetRelations()
Unset all the loaded relations for the instance.
at line 1060
array
getTouchedRelations()
Get the relationships that are touched on save.
at line 1070
HasRelationships
setTouchedRelations(array $touches)
Set the relationships that are touched on save.