class BelongsToMany extends Relation (View source)

Methods

void
__construct( Builder $query, Model $parent, string $table, string $foreignKey, string $otherKey, string $relationName = null)

Create a new belongs to many relationship instance.

void
addConstraints()

Set the base constraints on the relation query.

void
addEagerConstraints( array $models)

Set the constraints for an eager load of the relation.

array
initRelation( array $models, string $relation)

Initialize the relation on a set of models.

array
match( array $models, Collection $results, string $relation)

Match the eagerly loaded results to their parents.

mixed
getResults()

Get the results of the relationship.

getEager()

Get the relationship for eager loading.

from Relation
void
touch()

Touch all of the related models for the relationship.

int
rawUpdate( array $attributes = array())

Run a raw update against the base query.

from Relation
getRelationCountQuery( Builder $query, Builder $parent)

Add the constraints for a relationship count query.

from Relation
getRelationQuery( Builder $query, Builder $parent, array|mixed $columns = array('*'))

Add the constraints for a relationship query.

static  mixed
noConstraints( Closure $callback)

Run a callback with constraints disabled on the relation.

from Relation
getQuery()

Get the underlying query for the relation.

from Relation
getBaseQuery()

Get the base query builder driving the Eloquent builder.

from Relation
getParent()

Get the parent model of the relation.

from Relation
string
getQualifiedParentKeyName()

Get the fully qualified parent key name.

from Relation
getRelated()

Get the related model of the relation.

from Relation
string
createdAt()

Get the name of the "created at" column.

string
updatedAt()

Get the name of the "updated at" column.

string
relatedUpdatedAt()

Get the name of the related model's "updated at" column.

from Relation
string
wrap( string $value)

Wrap the given value with the parent query's grammar.

from Relation
static  array
morphMap( array $map = null, bool $merge = true)

Set or get the morph map for polymorphic relations.

from Relation
mixed
__call( string $method, array $parameters)

Handle dynamic method calls to the relationship.

from Relation
void
__clone()

Force a clone of the underlying query builder when cloning.

from Relation
wherePivot( string $column, string $operator = null, mixed $value = null, string $boolean = 'and')

Set a where clause for a pivot table column.

wherePivotIn( string $column, mixed $values, string $boolean = 'and', bool $not = false)

Set a "where in" clause for a pivot table column.

orWherePivot( string $column, string $operator = null, mixed $value = null)

Set an "or where" clause for a pivot table column.

orWherePivotIn( string $column, mixed $values)

Set an "or where in" clause for a pivot table column.

mixed
first( array $columns = array('*'))

Execute the query and get the first result.

firstOrFail( array $columns = array('*'))

Execute the query and get the first result or throw an exception.

get( array $columns = array('*'))

Execute the query as a "select" statement.

paginate( int $perPage = null, array $columns = array('*'), string $pageName = 'page', int|null $page = null)

Get a paginator for the "select" statement.

simplePaginate( int $perPage = null, array $columns = array('*'), string $pageName = 'page')

Paginate the given query into a simple paginator.

bool
chunk( int $count, callable $callback)

Chunk the results of the query.

getRelationQueryForSelfJoin( Builder $query, Builder $parent, array|mixed $columns = array('*'))

Add the constraints for a relationship query on the same table.

string
getRelationCountHash()

Get a relationship join table hash.

getRelatedIds()

Get all of the IDs for the related models.

save( Model $model, array $joining = array(), bool $touch = true)

Save a new model and attach it to the parent model.

array
saveMany( Collection|array $models, array $joinings = array())

Save an array of new models and attach them to the parent model.

find( mixed $id, array $columns = array('*'))

Find a related model by its primary key.

findMany( mixed $ids, array $columns = array('*'))

Find multiple related models by their primary keys.

findOrFail( mixed $id, array $columns = array('*'))

Find a related model by its primary key or throw an exception.

findOrNew( mixed $id, array $columns = array('*'))

Find a related model by its primary key or return new instance of the related model.

firstOrNew( array $attributes)

Get the first related model record matching the attributes or instantiate it.

firstOrCreate( array $attributes, array $joining = array(), bool $touch = true)

Get the first related record matching the attributes or create it.

updateOrCreate( array $attributes, array $values = array(), array $joining = array(), bool $touch = true)

Create or update a related record matching the attributes, and fill it with values.

create( array $attributes, array $joining = array(), bool $touch = true)

Create a new instance of the related model.

array
createMany( array $records, array $joinings = array())

Create an array of new instances of the related models.

array
sync( Collection|array $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models.

int
updateExistingPivot( mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table.

void
attach( mixed $id, array $attributes = array(), bool $touch = true)

Attach a model to the parent.

int
detach( int|array $ids = array(), bool $touch = true)

Detach models from the relationship.

void
touchIfTouching()

If we're touching the parent model, touch.

newPivotStatement()

Get a new plain query builder for the pivot table.

newPivotStatementForId( mixed $id)

Get a new pivot statement for a given "other" ID.

newPivot( array $attributes = array(), bool $exists = false)

Create a new pivot model instance.

newExistingPivot( array $attributes = array())

Create a new existing pivot model instance.

$this
withPivot( array|mixed $columns)

Set the columns on the pivot table to retrieve.

withTimestamps( mixed $createdAt = null, mixed $updatedAt = null)

Specify that the pivot table has creation and update timestamps.

string
getRelatedFreshUpdate()

Get the related model's updated at column name.

string
getHasCompareKey()

Get the key for comparing against the parent key in "has" query.

string
getForeignKey()

Get the fully qualified foreign key for the relation.

string
getOtherKey()

Get the fully qualified "other key" for the relation.

string
getTable()

Get the intermediate table for the relationship.

string
getRelationName()

Get the relationship name for the relationship.

Details

at line line 81
void __construct( Builder $query, Model $parent, string $table, string $foreignKey, string $otherKey, string $relationName = null)

Create a new belongs to many relationship instance.

Parameters

Builder $query
Model $parent
string $table
string $foreignKey
string $otherKey
string $relationName

Return Value

void

at line line 323
void addConstraints()

Set the base constraints on the relation query.

Return Value

void

at line line 471
void addEagerConstraints( array $models)

Set the constraints for an eager load of the relation.

Parameters

array $models

Return Value

void

at line line 483
array initRelation( array $models, string $relation)

Initialize the relation on a set of models.

Parameters

array $models
string $relation

Return Value

array

at line line 500
array match( array $models, Collection $results, string $relation)

Match the eagerly loaded results to their parents.

Parameters

array $models
Collection $results
string $relation

Return Value

array

at line line 96
mixed getResults()

Get the results of the relationship.

Return Value

mixed

in Relation at line line 111
Collection getEager()

Get the relationship for eager loading.

Return Value

Collection

at line line 547
void touch()

Touch all of the related models for the relationship.

E.g.: Touch all roles associated with this user.

Return Value

void

in Relation at line line 134
int rawUpdate( array $attributes = array())

Run a raw update against the base query.

Parameters

array $attributes

Return Value

int

in Relation at line line 146
Builder getRelationCountQuery( Builder $query, Builder $parent)

Add the constraints for a relationship count query.

Parameters

Builder $query
Builder $parent

Return Value

Builder

at line line 340
Builder getRelationQuery( Builder $query, Builder $parent, array|mixed $columns = array('*'))

Add the constraints for a relationship query.

Parameters

Builder $query
Builder $parent
array|mixed $columns

Return Value

Builder

in Relation at line line 174
static mixed noConstraints( Closure $callback)

Run a callback with constraints disabled on the relation.

Parameters

Closure $callback

Return Value

mixed

in Relation at line line 212
Builder getQuery()

Get the underlying query for the relation.

Return Value

Builder

in Relation at line line 222
Builder getBaseQuery()

Get the base query builder driving the Eloquent builder.

Return Value

Builder

in Relation at line line 232
Model getParent()

Get the parent model of the relation.

Return Value

Model

in Relation at line line 242
string getQualifiedParentKeyName()

Get the fully qualified parent key name.

Return Value

string

in Relation at line line 252
Model getRelated()

Get the related model of the relation.

Return Value

Model

at line line 1216
string createdAt()

Get the name of the "created at" column.

Return Value

string

at line line 1226
string updatedAt()

Get the name of the "updated at" column.

Return Value

string

in Relation at line line 282
string relatedUpdatedAt()

Get the name of the related model's "updated at" column.

Return Value

string

in Relation at line line 293
string wrap( string $value)

Wrap the given value with the parent query's grammar.

Parameters

string $value

Return Value

string

in Relation at line line 305
static array morphMap( array $map = null, bool $merge = true)

Set or get the morph map for polymorphic relations.

Parameters

array $map
bool $merge

Return Value

array

in Relation at line line 342
mixed __call( string $method, array $parameters)

Handle dynamic method calls to the relationship.

Parameters

string $method
array $parameters

Return Value

mixed

in Relation at line line 358
void __clone()

Force a clone of the underlying query builder when cloning.

Return Value

void

at line line 110
BelongsToMany wherePivot( string $column, string $operator = null, mixed $value = null, string $boolean = 'and')

Set a where clause for a pivot table column.

Parameters

string $column
string $operator
mixed $value
string $boolean

Return Value

BelongsToMany

at line line 126
BelongsToMany wherePivotIn( string $column, mixed $values, string $boolean = 'and', bool $not = false)

Set a "where in" clause for a pivot table column.

Parameters

string $column
mixed $values
string $boolean
bool $not

Return Value

BelongsToMany

at line line 141
BelongsToMany orWherePivot( string $column, string $operator = null, mixed $value = null)

Set an "or where" clause for a pivot table column.

Parameters

string $column
string $operator
mixed $value

Return Value

BelongsToMany

at line line 153
BelongsToMany orWherePivotIn( string $column, mixed $values)

Set an "or where in" clause for a pivot table column.

Parameters

string $column
mixed $values

Return Value

BelongsToMany

at line line 164
mixed first( array $columns = array('*'))

Execute the query and get the first result.

Parameters

array $columns

Return Value

mixed

at line line 179
Model|BelongsToMany firstOrFail( array $columns = array('*'))

Execute the query and get the first result or throw an exception.

Parameters

array $columns

Return Value

Model|BelongsToMany

Exceptions

ModelNotFoundException

at line line 194
Collection get( array $columns = array('*'))

Execute the query as a "select" statement.

Parameters

array $columns

Return Value

Collection

at line line 228
LengthAwarePaginator paginate( int $perPage = null, array $columns = array('*'), string $pageName = 'page', int|null $page = null)

Get a paginator for the "select" statement.

Parameters

int $perPage
array $columns
string $pageName
int|null $page

Return Value

LengthAwarePaginator

at line line 247
Paginator simplePaginate( int $perPage = null, array $columns = array('*'), string $pageName = 'page')

Paginate the given query into a simple paginator.

Parameters

int $perPage
array $columns
string $pageName

Return Value

Paginator

at line line 265
bool chunk( int $count, callable $callback)

Chunk the results of the query.

Parameters

int $count
callable $callback

Return Value

bool

at line line 359
Builder getRelationQueryForSelfJoin( Builder $query, Builder $parent, array|mixed $columns = array('*'))

Add the constraints for a relationship query on the same table.

Parameters

Builder $query
Builder $parent
array|mixed $columns

Return Value

Builder

at line line 377
string getRelationCountHash()

Get a relationship join table hash.

Return Value

string

at line line 568
Collection getRelatedIds()

Get all of the IDs for the related models.

Return Value

Collection

at line line 585
Model save( Model $model, array $joining = array(), bool $touch = true)

Save a new model and attach it to the parent model.

Parameters

Model $model
array $joining
bool $touch

Return Value

Model

at line line 601
array saveMany( Collection|array $models, array $joinings = array())

Save an array of new models and attach them to the parent model.

Parameters

Collection|array $models
array $joinings

Return Value

array

at line line 619
Model|Collection|null find( mixed $id, array $columns = array('*'))

Find a related model by its primary key.

Parameters

mixed $id
array $columns

Return Value

Model|Collection|null

at line line 637
Collection findMany( mixed $ids, array $columns = array('*'))

Find multiple related models by their primary keys.

Parameters

mixed $ids
array $columns

Return Value

Collection

at line line 657
Model|Collection findOrFail( mixed $id, array $columns = array('*'))

Find a related model by its primary key or throw an exception.

Parameters

mixed $id
array $columns

Return Value

Model|Collection

Exceptions

ModelNotFoundException

at line line 679
Collection|Model findOrNew( mixed $id, array $columns = array('*'))

Find a related model by its primary key or return new instance of the related model.

Parameters

mixed $id
array $columns

Return Value

Collection|Model

at line line 694
Model firstOrNew( array $attributes)

Get the first related model record matching the attributes or instantiate it.

Parameters

array $attributes

Return Value

Model

at line line 711
Model firstOrCreate( array $attributes, array $joining = array(), bool $touch = true)

Get the first related record matching the attributes or create it.

Parameters

array $attributes
array $joining
bool $touch

Return Value

Model

at line line 729
Model updateOrCreate( array $attributes, array $values = array(), array $joining = array(), bool $touch = true)

Create or update a related record matching the attributes, and fill it with values.

Parameters

array $attributes
array $values
array $joining
bool $touch

Return Value

Model

at line line 750
Model create( array $attributes, array $joining = array(), bool $touch = true)

Create a new instance of the related model.

Parameters

array $attributes
array $joining
bool $touch

Return Value

Model

at line line 771
array createMany( array $records, array $joinings = array())

Create an array of new instances of the related models.

Parameters

array $records
array $joinings

Return Value

array

at line line 791
array sync( Collection|array $ids, bool $detaching = true)

Sync the intermediate tables with a list of IDs or collection of models.

Parameters

Collection|array $ids
bool $detaching

Return Value

array

at line line 898
int updateExistingPivot( mixed $id, array $attributes, bool $touch = true)

Update an existing pivot record on the table.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

int

at line line 921
void attach( mixed $id, array $attributes = array(), bool $touch = true)

Attach a model to the parent.

Parameters

mixed $id
array $attributes
bool $touch

Return Value

void

at line line 1054
int detach( int|array $ids = array(), bool $touch = true)

Detach models from the relationship.

Parameters

int|array $ids
bool $touch

Return Value

int

at line line 1088
void touchIfTouching()

If we're touching the parent model, touch.

Return Value

void

at line line 1140
Builder newPivotStatement()

Get a new plain query builder for the pivot table.

Return Value

Builder

at line line 1151
Builder newPivotStatementForId( mixed $id)

Get a new pivot statement for a given "other" ID.

Parameters

mixed $id

Return Value

Builder

at line line 1163
Pivot newPivot( array $attributes = array(), bool $exists = false)

Create a new pivot model instance.

Parameters

array $attributes
bool $exists

Return Value

Pivot

at line line 1176
Pivot newExistingPivot( array $attributes = array())

Create a new existing pivot model instance.

Parameters

array $attributes

Return Value

Pivot

at line line 1187
$this withPivot( array|mixed $columns)

Set the columns on the pivot table to retrieve.

Parameters

array|mixed $columns

Return Value

$this

at line line 1203
BelongsToMany withTimestamps( mixed $createdAt = null, mixed $updatedAt = null)

Specify that the pivot table has creation and update timestamps.

Parameters

mixed $createdAt
mixed $updatedAt

Return Value

BelongsToMany

at line line 1236
string getRelatedFreshUpdate()

Get the related model's updated at column name.

Return Value

string

at line line 1246
string getHasCompareKey()

Get the key for comparing against the parent key in "has" query.

Return Value

string

at line line 1256
string getForeignKey()

Get the fully qualified foreign key for the relation.

Return Value

string

at line line 1266
string getOtherKey()

Get the fully qualified "other key" for the relation.

Return Value

string

at line line 1276
string getTable()

Get the intermediate table for the relationship.

Return Value

string

at line line 1286
string getRelationName()

Get the relationship name for the relationship.

Return Value

string