HasRelationships

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 array $manyMethods The many to many relationship methods.

Methods

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

Define a one-to-one relationship.

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

Instantiate a new HasOne relationship.

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

Define a polymorphic one-to-one relationship.

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

Instantiate a new MorphOne relationship.

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

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

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

Instantiate a new BelongsTo relationship.

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

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

MorphTo morphEagerTo(string $name, string $type, string $id, string $ownerKey)

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

MorphTo morphInstanceTo(string $target, string $name, string $type, string $id, string $ownerKey)

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

MorphTo newMorphTo(Builder $query, Model $parent, string $foreignKey, string $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.

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

Define a one-to-many relationship.

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

Instantiate a new HasMany relationship.

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.

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

Instantiate a new HasManyThrough relationship.

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

Define a polymorphic one-to-many relationship.

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

Instantiate a new MorphMany relationship.

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

Define a many-to-many relationship.

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

Instantiate a new BelongsToMany relationship.

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

Define a polymorphic many-to-many relationship.

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

Instantiate a new MorphToMany relationship.

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

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

string guessBelongsToManyRelation()

Get the relationship name of the belongs to many.

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.

mixed newRelatedInstance(string $class)

Create a new model instance for a related 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.

array getTouchedRelations()

Get the relationships that are touched on save.

$this setTouchedRelations(array $touches)

Set the relationships that are touched on save.

Details

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

Define a one-to-one relationship.

Parameters

string $related
string $foreignKey
string $localKey

Return Value

HasOne

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

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

Define a polymorphic one-to-one relationship.

Parameters

string $related
string $name
string $type
string $id
string $localKey

Return Value

MorphOne

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

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

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

Parameters

string $related
string $foreignKey
string $ownerKey
string $relation

Return Value

BelongsTo

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

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

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

Parameters

string $name
string $type
string $id
string $ownerKey

Return Value

MorphTo

protected MorphTo morphEagerTo(string $name, string $type, string $id, string $ownerKey)

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

Parameters

string $name
string $type
string $id
string $ownerKey

Return Value

MorphTo

protected MorphTo morphInstanceTo(string $target, string $name, string $type, string $id, string $ownerKey)

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

Parameters

string $target
string $name
string $type
string $id
string $ownerKey

Return Value

MorphTo

protected MorphTo newMorphTo(Builder $query, Model $parent, string $foreignKey, string $ownerKey, string $type, string $relation)

Instantiate a new MorphTo relationship.

Parameters

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

Return Value

MorphTo

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

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

Define a one-to-many relationship.

Parameters

string $related
string $foreignKey
string $localKey

Return Value

HasMany

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

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

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

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

Define a polymorphic one-to-many relationship.

Parameters

string $related
string $name
string $type
string $id
string $localKey

Return Value

MorphMany

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

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

Define a many-to-many relationship.

Parameters

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

Return Value

BelongsToMany

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

Instantiate a new BelongsToMany relationship.

Parameters

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

Return Value

BelongsToMany

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

Define a polymorphic many-to-many relationship.

Parameters

string $related
string $name
string $table
string $foreignPivotKey
string $relatedPivotKey
string $parentKey
string $relatedKey
bool $inverse

Return Value

MorphToMany

protected MorphToMany newMorphToMany(Builder $query, Model $parent, string $name, string $table, string $foreignPivotKey, string $relatedPivotKey, string $parentKey, string $relatedKey, string $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 $relationName
bool $inverse

Return Value

MorphToMany

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

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

Parameters

string $related
string $name
string $table
string $foreignPivotKey
string $relatedPivotKey
string $parentKey
string $relatedKey

Return Value

MorphToMany

protected string guessBelongsToManyRelation()

Get the relationship name of the belongs to many.

Return Value

string

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 mixed newRelatedInstance(string $class)

Create a new model instance for a related model.

Parameters

string $class

Return Value

mixed

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

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

© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html