class Illuminate / Database / Eloquent / Relations / BelongsToMany

BelongsToMany

Extends

Uses

Properties

protected

$table string

The intermediate table for the relation.

Default: null
protected

$foreignPivotKey string

The foreign key of the parent model.

Default: null
protected

$relatedPivotKey string

The associated key of the relation.

Default: null
protected

$parentKey string

The key name of the parent model.

Default: null
protected

$relatedKey string

The key name of the related model.

Default: null
protected

$relationName string

The "name" of the relationship.

Default: null
protected

$pivotColumns array

The pivot table columns to retrieve.

Default: []
protected

$pivotWheres array

Any pivot table restrictions for where clauses.

Default: []
protected

$pivotWhereIns array

Any pivot table restrictions for whereIn clauses.

Default: []
protected

$pivotWhereNulls array

Any pivot table restrictions for whereNull clauses.

Default: []
protected

$pivotValues array

The default values for the pivot columns.

Default: []
public

$withTimestamps bool

Indicates if timestamps are available on the pivot table.

Default: false
protected

$pivotCreatedAt string

The custom pivot table column for the created_at timestamp.

Default: null
protected

$pivotUpdatedAt string

The custom pivot table column for the updated_at timestamp.

Default: null
protected

$using string

The class name of the custom pivot model to use for the relationship.

Default: null
protected

$accessor string

The name of the accessor to use for the "pivot" relationship.

Default: 'pivot'

Methods

public

__construct ( Builder $query , Model $parent , $table , $foreignPivotKey , $relatedPivotKey , $parentKey , $relatedKey , $relationName ) : void

Create a new belongs to many relationship instance.

Parameters

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

resolveTableName ( $table ) : string

Attempt to resolve the intermediate table name from the given string.

Parameters

  • $table string
public

addConstraints ( ) : void

Set the base constraints on the relation query.

protected

performJoin ( $query ) : $this

Set the join clause for the relation query.

Parameters

protected

addWhereConstraints ( ) : $this

Set the where clause for the relation query.

public

addEagerConstraints ( array $models ) : void

Set the constraints for an eager load of the relation.

Parameters

  • $models array
public

initRelation ( array $models , $relation ) : array

Initialize the relation on a set of models.

Parameters

  • $models array
  • $relation string
public

match ( array $models , Illuminate\...\Collection $results , $relation ) : array

Match the eagerly loaded results to their parents.

Parameters

  • $models array
  • $results Illuminate\...\Collection
  • $relation string
protected

buildDictionary ( Illuminate\...\Collection $results ) : array

Build model dictionary keyed by the relation's foreign key.

Parameters

  • $results Illuminate\...\Collection
public

getPivotClass ( ) : string

Get the class being used for pivot models.

public

using ( $class ) : $this

Specify the custom pivot model to use for the relationship.

Parameters

  • $class string
public

as ( $accessor ) : $this

Specify the custom pivot accessor to use for the relationship.

Parameters

  • $accessor string
public

wherePivot ( $column , $operator , $value , $boolean ) : $this

Set a where clause for a pivot table column.

Parameters

  • $column string
  • $operator mixed
  • $value mixed
  • $boolean string
public

wherePivotBetween ( $column , array $values , $boolean , $not ) : $this

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

Parameters

  • $column string
  • $values array
  • $boolean string
  • $not bool
public

orWherePivotBetween ( $column , array $values ) : $this

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

Parameters

  • $column string
  • $values array
public

wherePivotNotBetween ( $column , array $values , $boolean ) : $this

Set a "where pivot not between" clause for a pivot table column.

Parameters

  • $column string
  • $values array
  • $boolean string
public

orWherePivotNotBetween ( $column , array $values ) : $this

Set a "or where not between" clause for a pivot table column.

Parameters

  • $column string
  • $values array
public

wherePivotIn ( $column , $values , $boolean , $not ) : $this

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

Parameters

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

orWherePivot ( $column , $operator , $value ) : $this

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

Parameters

  • $column string
  • $operator mixed
  • $value mixed
public

withPivotValue ( $column , $value ) : $this

Set a where clause for a pivot table column.

Parameters

  • $column string|array
  • $value mixed
public

orWherePivotIn ( $column , $values ) : $this

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

Parameters

  • $column string
  • $values mixed
public

wherePivotNotIn ( $column , $values , $boolean ) : $this

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

Parameters

  • $column string
  • $values mixed
  • $boolean string
public

orWherePivotNotIn ( $column , $values ) : $this

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

Parameters

  • $column string
  • $values mixed
public

wherePivotNull ( $column , $boolean , $not ) : $this

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

Parameters

  • $column string
  • $boolean string
  • $not bool
public

wherePivotNotNull ( $column , $boolean ) : $this

Set a "where not null" clause for a pivot table column.

Parameters

  • $column string
  • $boolean string
public

orWherePivotNull ( $column , $not ) : $this

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

Parameters

  • $column string
  • $not bool
public

orWherePivotNotNull ( $column ) : $this

Set a "or where not null" clause for a pivot table column.

Parameters

  • $column string
public

orderByPivot ( $column , $direction ) : $this

Add an "order by" clause for a pivot table column.

Parameters

  • $column string
  • $direction string
public

findOrNew ( $id , $columns ) : Illuminate\...\Collection|Model

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

Parameters

  • $id mixed
  • $columns array
public

firstOrNew ( array $attributes , array $values ) : Model

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

Parameters

  • $attributes array
  • $values array
public

firstOrCreate ( array $attributes , array $values , array $joining , $touch ) : Model

Get the first record matching the attributes. If the record is not found, create it.

Parameters

  • $attributes array
  • $values array
  • $joining array
  • $touch bool
public

createOrFirst ( array $attributes , array $values , array $joining , $touch ) : Model

Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record.

Parameters

  • $attributes array
  • $values array
  • $joining array
  • $touch bool
public

updateOrCreate ( array $attributes , array $values , array $joining , $touch ) : Model

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

Parameters

  • $attributes array
  • $values array
  • $joining array
  • $touch bool
public

find ( $id , $columns ) : Model |Illuminate\...\Collection|null

Find a related model by its primary key.

Parameters

  • $id mixed
  • $columns array
public

findMany ( $ids , $columns ) : Illuminate\...\Collection

Find multiple related models by their primary keys.

Parameters

  • $ids Illuminate\...\Arrayable|array
  • $columns array
public

findOrFail ( $id , $columns ) : Model |Illuminate\...\Collection

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

Parameters

  • $id mixed
  • $columns array
public

findOr ( $id , $columns , Closure $callback ) : Model |Illuminate\...\Collection|mixed

Find a related model by its primary key or call a callback.

Parameters

public

firstWhere ( $column , $operator , $value , $boolean ) : Model |static

Add a basic where clause to the query, and return the first result.

Parameters

  • $column Closure |string|array
  • $operator mixed
  • $value mixed
  • $boolean string
public

first ( $columns ) : mixed

Execute the query and get the first result.

Parameters

  • $columns array
public

firstOrFail ( $columns ) : Model |static

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

Parameters

  • $columns array
public

firstOr ( $columns , Closure $callback ) : Model |static|mixed

Execute the query and get the first result or call a callback.

Parameters

public

getResults ( ) : mixed

Get the results of the relationship.

public

get ( $columns ) : Illuminate\...\Collection

Execute the query as a "select" statement.

Parameters

  • $columns array
protected

shouldSelect ( array $columns ) : array

Get the select columns for the relation query.

Parameters

  • $columns array
protected

aliasedPivotColumns ( ) : array

Get the pivot columns for the relation.

public

paginate ( $perPage , $columns , $pageName , $page ) : LengthAwarePaginator

Get a paginator for the "select" statement.

Parameters

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

simplePaginate ( $perPage , $columns , $pageName , $page ) : Paginator

Paginate the given query into a simple paginator.

Parameters

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

cursorPaginate ( $perPage , $columns , $cursorName , $cursor ) : CursorPaginator

Paginate the given query into a cursor paginator.

Parameters

  • $perPage int|null
  • $columns array
  • $cursorName string
  • $cursor string|null
public

chunk ( $count , callable $callback ) : bool

Chunk the results of the query.

Parameters

  • $count int
  • $callback callable
public

chunkById ( $count , callable $callback , $column , $alias ) : bool

Chunk the results of a query by comparing numeric IDs.

Parameters

  • $count int
  • $callback callable
  • $column string|null
  • $alias string|null
public

each ( callable $callback , $count ) : bool

Execute a callback over each item while chunking.

Parameters

  • $callback callable
  • $count int
public

lazy ( $chunkSize ) : Illuminate\...\LazyCollection

Query lazily, by chunks of the given size.

Parameters

  • $chunkSize int
public

lazyById ( $chunkSize , $column , $alias ) : Illuminate\...\LazyCollection

Query lazily, by chunking the results of a query by comparing IDs.

Parameters

  • $chunkSize int
  • $column string|null
  • $alias string|null
public

cursor ( ) : Illuminate\...\LazyCollection

Get a lazy collection for the given query.

protected

prepareQueryBuilder ( ) : Builder

Prepare the query builder for query execution.

protected

hydratePivotRelation ( array $models ) : void

Hydrate the pivot table relationship on the models.

Parameters

  • $models array
protected

migratePivotAttributes ( Model $model ) : array

Get the pivot attributes from a model.

Parameters

public

touchIfTouching ( ) : void

If we're touching the parent model, touch.

protected

touchingParent ( ) : bool

Determine if we should touch the parent on sync.

protected

guessInverseRelation ( ) : string

Attempt to guess the name of the inverse of the relation.

public

touch ( ) : void

Touch all of the related models for the relationship.

public

allRelatedIds ( ) : Illuminate\...\Collection

Get all of the IDs for the related models.

public

save ( Model $model , array $pivotAttributes , $touch ) : Model

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

Parameters

  • $model Model
  • $pivotAttributes array
  • $touch bool
public

saveQuietly ( Model $model , array $pivotAttributes , $touch ) : Model

Save a new model without raising any events and attach it to the parent model.

Parameters

  • $model Model
  • $pivotAttributes array
  • $touch bool
public

saveMany ( $models , array $pivotAttributes ) : array

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

Parameters

  • $models Illuminate\...\Collection|array
  • $pivotAttributes array
public

saveManyQuietly ( $models , array $pivotAttributes ) : array

Save an array of new models without raising any events and attach them to the parent model.

Parameters

  • $models Illuminate\...\Collection|array
  • $pivotAttributes array
public

create ( array $attributes , array $joining , $touch ) : Model

Create a new instance of the related model.

Parameters

  • $attributes array
  • $joining array
  • $touch bool
public

createMany ( iterable $records , array $joinings ) : array

Create an array of new instances of the related models.

Parameters

  • $records iterable
  • $joinings array
public

getRelationExistenceQuery ( Builder $query , Builder $parentQuery , $columns ) : Builder

Add the constraints for a relationship query.

Parameters

public

getRelationExistenceQueryForSelfJoin ( Builder $query , Builder $parentQuery , $columns ) : Builder

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

Parameters

public

getExistenceCompareKey ( ) : string

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

public

withTimestamps ( $createdAt , $updatedAt ) : $this

Specify that the pivot table has creation and update timestamps.

Parameters

  • $createdAt mixed
  • $updatedAt mixed
public

createdAt ( ) : string

Get the name of the "created at" column.

public

updatedAt ( ) : string

Get the name of the "updated at" column.

public

getForeignPivotKeyName ( ) : string

Get the foreign key for the relation.

public

getQualifiedForeignPivotKeyName ( ) : string

Get the fully qualified foreign key for the relation.

public

getRelatedPivotKeyName ( ) : string

Get the "related key" for the relation.

public

getQualifiedRelatedPivotKeyName ( ) : string

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

public

getParentKeyName ( ) : string

Get the parent key for the relationship.

public

getQualifiedParentKeyName ( ) : string

Get the fully qualified parent key name for the relation.

public

getRelatedKeyName ( ) : string

Get the related key for the relationship.

public

getQualifiedRelatedKeyName ( ) : string

Get the fully qualified related key name for the relation.

public

getTable ( ) : string

Get the intermediate table for the relationship.

public

getRelationName ( ) : string

Get the relationship name for the relationship.

public

getPivotAccessor ( ) : string

Get the name of the pivot accessor for this relationship.

public

getPivotColumns ( ) : array

Get the pivot columns for this relationship.

public

qualifyPivotColumn ( $column ) : string

Qualify the given column name by the pivot table.

Parameters

  • $column string