trait Illuminate / Database / Eloquent / Concerns / HasRelationships
You're browsing the documentation for an upcoming version of Laravel. The documentation and features of this release are subject to change.

HasRelationships

Properties

protected

$relations array

The loaded relationships for the model.

Default: []
protected

$touches array

The relationships that should be touched on save.

Default: []
public static

$manyMethods string[]

The many to many relationship methods.

Default: array
protected static

$relationResolvers array

The relation resolver callbacks.

Default: []

Methods

public

relationResolver ( $class , $key ) : mixed

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

Parameters

  • $class string
  • $key string
public static

resolveRelationUsing ( $name , Closure $callback ) : void

Define a dynamic relation resolver.

Parameters

public

hasOne ( $related , $foreignKey , $localKey ) : HasOne

Define a one-to-one relationship.

Parameters

  • $related string
  • $foreignKey string|null
  • $localKey string|null
protected

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

Instantiate a new HasOne relationship.

Parameters

public

hasOneThrough ( $related , $through , $firstKey , $secondKey , $localKey , $secondLocalKey ) : HasOneThrough

Define a has-one-through relationship.

Parameters

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

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

Instantiate a new HasOneThrough relationship.

Parameters

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

morphOne ( $related , $name , $type , $id , $localKey ) : MorphOne

Define a polymorphic one-to-one relationship.

Parameters

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

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

Instantiate a new MorphOne relationship.

Parameters

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

belongsTo ( $related , $foreignKey , $ownerKey , $relation ) : BelongsTo

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

Parameters

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

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

Instantiate a new BelongsTo relationship.

Parameters

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

morphTo ( $name , $type , $id , $ownerKey ) : MorphTo

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

Parameters

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

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

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

Parameters

  • $name string
  • $type string
  • $id string
  • $ownerKey string
protected

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

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

Parameters

  • $target string
  • $name string
  • $type string
  • $id string
  • $ownerKey string
protected

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

Instantiate a new MorphTo relationship.

Parameters

  • $query Builder
  • $parent Model
  • $foreignKey string
  • $ownerKey string
  • $type string
  • $relation string
public static

getActualClassNameForMorph ( $class ) : string

Retrieve the actual class name for a given morph class.

Parameters

  • $class string
protected

guessBelongsToRelation ( ) : string

Guess the "belongs to" relationship name.

public

through ( $relationship ) : PendingHasThroughRelationship

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

Parameters

public

hasMany ( $related , $foreignKey , $localKey ) : HasMany

Define a one-to-many relationship.

Parameters

  • $related string
  • $foreignKey string|null
  • $localKey string|null
protected

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

Instantiate a new HasMany relationship.

Parameters

public

hasManyThrough ( $related , $through , $firstKey , $secondKey , $localKey , $secondLocalKey ) : HasManyThrough

Define a has-many-through relationship.

Parameters

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

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

Instantiate a new HasManyThrough relationship.

Parameters

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

morphMany ( $related , $name , $type , $id , $localKey ) : MorphMany

Define a polymorphic one-to-many relationship.

Parameters

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

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

Instantiate a new MorphMany relationship.

Parameters

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

belongsToMany ( $related , $table , $foreignPivotKey , $relatedPivotKey , $parentKey , $relatedKey , $relation ) : BelongsToMany

Define a many-to-many relationship.

Parameters

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

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

Instantiate a new BelongsToMany relationship.

Parameters

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

morphToMany ( $related , $name , $table , $foreignPivotKey , $relatedPivotKey , $parentKey , $relatedKey , $relation , $inverse ) : MorphToMany

Define a polymorphic many-to-many relationship.

Parameters

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

newMorphToMany ( Builder $query , Model $parent , $name , $table , $foreignPivotKey , $relatedPivotKey , $parentKey , $relatedKey , $relationName , $inverse ) : MorphToMany

Instantiate a new MorphToMany relationship.

Parameters

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

morphedByMany ( $related , $name , $table , $foreignPivotKey , $relatedPivotKey , $parentKey , $relatedKey , $relation ) : MorphToMany

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

Parameters

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

guessBelongsToManyRelation ( ) : string|null

Get the relationship name of the belongsToMany relationship.

public

joiningTable ( $related , $instance ) : string

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

Parameters

  • $related string
  • $instance Model |null
public

joiningTableSegment ( ) : string

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

public

touches ( $relation ) : bool

Determine if the model touches a given relation.

Parameters

  • $relation string
public

touchOwners ( ) : void

Touch the owning relations of the model.

protected

getMorphs ( $name , $type , $id ) : array

Get the polymorphic relationship columns.

Parameters

  • $name string
  • $type string
  • $id string
public

getMorphClass ( ) : string

Get the class name for polymorphic relations.

protected

newRelatedInstance ( $class ) : mixed

Create a new model instance for a related model.

Parameters

  • $class string
protected

newRelatedThroughInstance ( $class ) : mixed

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

Parameters

  • $class string
public

getRelations ( ) : array

Get all the loaded relations for the instance.

public

getRelation ( $relation ) : mixed

Get a specified relationship.

Parameters

  • $relation string
public

relationLoaded ( $key ) : bool

Determine if the given relation is loaded.

Parameters

  • $key string
public

setRelation ( $relation , $value ) : $this

Set the given relationship on the model.

Parameters

  • $relation string
  • $value mixed
public

unsetRelation ( $relation ) : $this

Unset a loaded relationship.

Parameters

  • $relation string
public

setRelations ( array $relations ) : $this

Set the entire relations array on the model.

Parameters

  • $relations array
public

withoutRelations ( ) : $this

Duplicate the instance and unset all the loaded relations.

public

unsetRelations ( ) : $this

Unset all the loaded relations for the instance.

public

getTouchedRelations ( ) : array

Get the relationships that are touched on save.

public

setTouchedRelations ( array $touches ) : $this

Set the relationships that are touched on save.

Parameters

  • $touches array