Documentation

HasRelationships

Table of Contents

$attachMany  : mixed
protected $attachMany = [ 'pictures' => ['October\Rain\Database\Attach\File', 'name'=> 'imageable'] ];
$attachOne  : mixed
protected $attachOne = [ 'picture' => ['October\Rain\Database\Attach\File', 'public' => false] ];
$belongsTo  : mixed
protected $belongsTo = [ 'parent' => ['Category', 'key' => 'parent_id'] ];
$belongsToMany  : mixed
protected $belongsToMany = [ 'groups' => ['Group', 'table'=> 'join_groups_users'] ];
$hasMany  : array<string|int, mixed>
Cleaner declaration of relationships.
$hasManyThrough  : mixed
protected $attachMany = [ 'pictures' => ['Picture', 'name'=> 'imageable'] ];
$hasOne  : mixed
protected $hasOne = [ 'owner' => ['User', 'key' => 'user_id'] ];
$morphedByMany  : mixed
$morphMany  : mixed
protected $morphMany = [ 'log' => ['History', 'name' => 'user'] ];
$morphOne  : mixed
protected $morphOne = [ 'log' => ['History', 'name' => 'user'] ];
$morphTo  : mixed
protected $morphTo = [ 'pictures' => [] ];
$morphToMany  : mixed
protected $morphToMany = [ 'tag' => ['Tag', 'table' => 'tagables', 'name' => 'tagable'] ];
$relationTypes  : array<string|int, mixed>
attachMany()  : MorphMany
Define an attachment one-to-many relationship.
attachOne()  : MorphOne
Define an attachment one-to-one relationship.
belongsTo()  : BelongsTo
Define an inverse one-to-one or many relationship.
belongsToMany()  : BelongsToMany
Define a many-to-many relationship.
getRelationDefinition()  : array<string|int, mixed>
Returns relationship details from a supplied name.
getRelationDefinitions()  : array<string|int, mixed>
Returns relationship details for all relations defined on this model.
getRelationType()  : string
Returns a relationship type based on a supplied name.
getRelationValue()  : mixed
Returns a relation key value(s), not as an object.
hasMany()  : HasMany
Define a one-to-many relationship.
hasManyThrough()  : HasMany
Define a has-many-through relationship.
hasOne()  : HasOne
Define a one-to-one relationship.
hasRelation()  : bool
Checks if model has a relationship by supplied name.
isRelationPushable()  : bool
Determines whether the specified relation should be saved when push() is called instead of save() on the model. Default: true.
makeRelation()  : string
Returns a relation class object
morphedByMany()  : MorphToMany
Define a polymorphic many-to-many inverse relationship.
morphMany()  : MorphMany
Define a polymorphic one-to-many relationship.
morphOne()  : MorphOne
Define a polymorphic one-to-one relationship.
morphTo()  : BelongsTo
Define an polymorphic, inverse one-to-one or many relationship.
morphToMany()  : MorphToMany
Define a polymorphic many-to-many relationship.
getRelationCaller()  : mixed
Finds the calling function name from the stack trace.
getRelationDefaults()  : array<string|int, mixed>
Returns default relation arguments for a given type.
handleRelation()  : Relation
Looks for the relation and does the correct magic as Eloquent would require inside relation methods. For more information, read the documentation of the mentioned property.
morphEagerTo()  : MorphTo
Define a polymorphic, inverse one-to-one or many relationship.
morphInstanceTo()  : MorphTo
Define a polymorphic, inverse one-to-one or many relationship.
setRelationValue()  : mixed
Sets a relation value directly from its attribute.
validateRelationArgs()  : mixed
Validate relation supplied arguments.

Properties

$attachMany

protected $attachMany = [ 'pictures' => ['October\Rain\Database\Attach\File', 'name'=> 'imageable'] ];

public mixed $attachMany = []

$attachOne

protected $attachOne = [ 'picture' => ['October\Rain\Database\Attach\File', 'public' => false] ];

public mixed $attachOne = []

$belongsTo

protected $belongsTo = [ 'parent' => ['Category', 'key' => 'parent_id'] ];

public mixed $belongsTo = []

$belongsToMany

protected $belongsToMany = [ 'groups' => ['Group', 'table'=> 'join_groups_users'] ];

public mixed $belongsToMany = []

$hasMany

Cleaner declaration of relationships.

public array<string|int, mixed> $hasMany = []

Uses a similar approach to the relation methods used by Eloquent, but as separate properties that make the class file less cluttered.

It should be declared with keys as the relation name, and value being a mixed array. The relation type $morphTo does not include a classname as the first value.

Example: class Order extends Model { protected $hasMany = [ 'items' => 'Item' ]; }

$hasManyThrough

protected $attachMany = [ 'pictures' => ['Picture', 'name'=> 'imageable'] ];

public mixed $hasManyThrough = []

$hasOne

protected $hasOne = [ 'owner' => ['User', 'key' => 'user_id'] ];

public mixed $hasOne = []

$morphMany

protected $morphMany = [ 'log' => ['History', 'name' => 'user'] ];

public mixed $morphMany = []

$morphOne

protected $morphOne = [ 'log' => ['History', 'name' => 'user'] ];

public mixed $morphOne = []

$morphTo

protected $morphTo = [ 'pictures' => [] ];

public mixed $morphTo = []

$morphToMany

protected $morphToMany = [ 'tag' => ['Tag', 'table' => 'tagables', 'name' => 'tagable'] ];

public mixed $morphToMany = []

$relationTypes

protected static array<string|int, mixed> $relationTypes = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphTo', 'morphOne', 'morphMany', 'morphToMany', 'morphedByMany', 'attachOne', 'attachMany', 'hasManyThrough']

Excepted relationship types, used to cycle and verify relationships.

Methods

attachMany()

Define an attachment one-to-many relationship.

public attachMany(mixed $related[, mixed $isPublic = null ][, mixed $localKey = null ][, mixed $relationName = null ]) : MorphMany

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$isPublic : mixed = null
$localKey : mixed = null
$relationName : mixed = null
Return values
MorphMany

attachOne()

Define an attachment one-to-one relationship.

public attachOne(mixed $related[, mixed $isPublic = true ][, mixed $localKey = null ][, mixed $relationName = null ]) : MorphOne

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$isPublic : mixed = true
$localKey : mixed = null
$relationName : mixed = null
Return values
MorphOne

belongsTo()

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

public belongsTo(mixed $related[, mixed $foreignKey = null ][, mixed $parentKey = null ][, mixed $relationName = null ]) : BelongsTo

Overridden from to allow the usage of the intermediary methods to handle the array.

Parameters
$related : mixed
$foreignKey : mixed = null
$parentKey : mixed = null
$relationName : mixed = null
Return values
BelongsTo

belongsToMany()

Define a many-to-many relationship.

public belongsToMany(mixed $related[, mixed $table = null ][, mixed $primaryKey = null ][, mixed $foreignKey = null ][, mixed $parentKey = null ][, mixed $relatedKey = null ][, mixed $relationName = null ]) : BelongsToMany

This code is almost a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$table : mixed = null
$primaryKey : mixed = null
$foreignKey : mixed = null
$parentKey : mixed = null
$relatedKey : mixed = null
$relationName : mixed = null
Return values
BelongsToMany

getRelationDefinition()

Returns relationship details from a supplied name.

public getRelationDefinition(string $name) : array<string|int, mixed>
Parameters
$name : string

Relation name

Return values
array<string|int, mixed>

getRelationDefinitions()

Returns relationship details for all relations defined on this model.

public getRelationDefinitions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getRelationType()

Returns a relationship type based on a supplied name.

public getRelationType(string $name) : string
Parameters
$name : string

Relation name

Return values
string

getRelationValue()

Returns a relation key value(s), not as an object.

public getRelationValue(mixed $relationName) : mixed
Parameters
$relationName : mixed
Return values
mixed

hasMany()

Define a one-to-many relationship.

public hasMany(mixed $related[, mixed $primaryKey = null ][, mixed $localKey = null ][, mixed $relationName = null ]) : HasMany

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$primaryKey : mixed = null
$localKey : mixed = null
$relationName : mixed = null
Return values
HasMany

hasManyThrough()

Define a has-many-through relationship.

public hasManyThrough(mixed $related, mixed $through[, mixed $primaryKey = null ][, mixed $throughKey = null ][, mixed $localKey = null ][, mixed $secondLocalKey = null ][, mixed $relationName = null ]) : HasMany

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$through : mixed
$primaryKey : mixed = null
$throughKey : mixed = null
$localKey : mixed = null
$secondLocalKey : mixed = null
$relationName : mixed = null
Return values
HasMany

hasOne()

Define a one-to-one relationship.

public hasOne(mixed $related[, mixed $primaryKey = null ][, mixed $localKey = null ][, mixed $relationName = null ]) : HasOne

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$primaryKey : mixed = null
$localKey : mixed = null
$relationName : mixed = null
Return values
HasOne

hasRelation()

Checks if model has a relationship by supplied name.

public hasRelation(string $name) : bool
Parameters
$name : string

Relation name

Return values
bool

isRelationPushable()

Determines whether the specified relation should be saved when push() is called instead of save() on the model. Default: true.

public isRelationPushable(string $name) : bool
Parameters
$name : string

Relation name

Return values
bool

makeRelation()

Returns a relation class object

public makeRelation(string $name) : string
Parameters
$name : string

Relation name

Return values
string

morphedByMany()

Define a polymorphic many-to-many inverse relationship.

public morphedByMany(mixed $related, mixed $name[, mixed $table = null ][, mixed $primaryKey = null ][, mixed $foreignKey = null ][, mixed $parentKey = null ][, mixed $relatedKey = null ][, mixed $relationName = null ]) : MorphToMany

This code is almost a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$name : mixed
$table : mixed = null
$primaryKey : mixed = null
$foreignKey : mixed = null
$parentKey : mixed = null
$relatedKey : mixed = null
$relationName : mixed = null
Return values
MorphToMany

morphMany()

Define a polymorphic one-to-many relationship.

public morphMany(mixed $related, mixed $name[, mixed $type = null ][, mixed $id = null ][, mixed $localKey = null ][, mixed $relationName = null ]) : MorphMany

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$name : mixed
$type : mixed = null
$id : mixed = null
$localKey : mixed = null
$relationName : mixed = null
Return values
MorphMany

morphOne()

Define a polymorphic one-to-one relationship.

public morphOne(mixed $related, mixed $name[, mixed $type = null ][, mixed $id = null ][, mixed $localKey = null ][, mixed $relationName = null ]) : MorphOne

This code is a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$name : mixed
$type : mixed = null
$id : mixed = null
$localKey : mixed = null
$relationName : mixed = null
Return values
MorphOne

morphTo()

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

public morphTo([mixed $name = null ][, mixed $type = null ][, mixed $id = null ]) : BelongsTo

Overridden from to allow the usage of the intermediary methods to handle the relation.

Parameters
$name : mixed = null
$type : mixed = null
$id : mixed = null
Return values
BelongsTo

morphToMany()

Define a polymorphic many-to-many relationship.

public morphToMany(mixed $related, mixed $name[, mixed $table = null ][, mixed $primaryKey = null ][, mixed $foreignKey = null ][, mixed $parentKey = null ][, mixed $relatedKey = null ][, mixed $inverse = false ][, mixed $relationName = null ]) : MorphToMany

This code is almost a duplicate of Eloquent but uses a Rain relation class.

Parameters
$related : mixed
$name : mixed
$table : mixed = null
$primaryKey : mixed = null
$foreignKey : mixed = null
$parentKey : mixed = null
$relatedKey : mixed = null
$inverse : mixed = false
$relationName : mixed = null
Return values
MorphToMany

getRelationCaller()

Finds the calling function name from the stack trace.

protected getRelationCaller() : mixed
Return values
mixed

getRelationDefaults()

Returns default relation arguments for a given type.

protected getRelationDefaults(string $type) : array<string|int, mixed>
Parameters
$type : string

Relation type

Return values
array<string|int, mixed>

handleRelation()

Looks for the relation and does the correct magic as Eloquent would require inside relation methods. For more information, read the documentation of the mentioned property.

protected handleRelation(string $relationName) : Relation
Parameters
$relationName : string

the relation key, camel-case version

Return values
Relation

morphEagerTo()

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

protected morphEagerTo(string $name, string $type, string $id) : MorphTo
Parameters
$name : string
$type : string
$id : string
Return values
MorphTo

morphInstanceTo()

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

protected morphInstanceTo(string $target, string $name, string $type, string $id) : MorphTo
Parameters
$target : string
$name : string
$type : string
$id : string
Return values
MorphTo

setRelationValue()

Sets a relation value directly from its attribute.

protected setRelationValue(mixed $relationName, mixed $value) : mixed
Parameters
$relationName : mixed
$value : mixed
Return values
mixed

validateRelationArgs()

Validate relation supplied arguments.

protected validateRelationArgs(mixed $relationName, mixed $optional[, mixed $required = [] ]) : mixed
Parameters
$relationName : mixed
$optional : mixed
$required : mixed = []
Return values
mixed

Search results