code
stringlengths 31
1.39M
| docstring
stringlengths 23
16.8k
| func_name
stringlengths 1
126
| language
stringclasses 1
value | repo
stringlengths 7
63
| path
stringlengths 7
166
| url
stringlengths 50
220
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
public function getRelationName()
{
return $this->relationName;
}
|
Get the name of the relationship.
@return string
|
getRelationName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function __construct(
Builder $query,
Model $parent,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName = null,
) {
$this->parentKey = $parentKey;
$this->relatedKey = $relatedKey;
$this->relationName = $relationName;
$this->relatedPivotKey = $relatedPivotKey;
$this->foreignPivotKey = $foreignPivotKey;
$this->table = $this->resolveTableName($table);
parent::__construct($query, $parent);
}
|
Create a new belongs to many relationship instance.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param TDeclaringModel $parent
@param string|class-string<TRelatedModel> $table
@param string $foreignPivotKey
@param string $relatedPivotKey
@param string $parentKey
@param string $relatedKey
@param string|null $relationName
|
__construct
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function resolveTableName($table)
{
if (! str_contains($table, '\\') || ! class_exists($table)) {
return $table;
}
$model = new $table;
if (! $model instanceof Model) {
return $table;
}
if (in_array(AsPivot::class, class_uses_recursive($model))) {
$this->using($table);
}
return $model->getTable();
}
|
Attempt to resolve the intermediate table name from the given string.
@param string $table
@return string
|
resolveTableName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function addConstraints()
{
$this->performJoin();
if (static::$constraints) {
$this->addWhereConstraints();
}
}
|
Set the base constraints on the relation query.
@return void
|
addConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function performJoin($query = null)
{
$query = $query ?: $this->query;
// We need to join to the intermediate table on the related model's primary
// key column with the intermediate table's foreign key for the related
// model instance. Then we can set the "where" for the parent models.
$query->join(
$this->table,
$this->getQualifiedRelatedKeyName(),
'=',
$this->getQualifiedRelatedPivotKeyName()
);
return $this;
}
|
Set the join clause for the relation query.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel>|null $query
@return $this
|
performJoin
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function addWhereConstraints()
{
$this->query->where(
$this->getQualifiedForeignPivotKeyName(), '=', $this->parent->{$this->parentKey}
);
return $this;
}
|
Set the where clause for the relation query.
@return $this
|
addWhereConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function buildDictionary(EloquentCollection $results)
{
// First we'll build a dictionary of child models keyed by the foreign key
// of the relation so that we will easily and quickly match them to the
// parents without having a possibly slow inner loop for every model.
$dictionary = [];
foreach ($results as $result) {
$value = $this->getDictionaryKey($result->{$this->accessor}->{$this->foreignPivotKey});
$dictionary[$value][] = $result;
}
return $dictionary;
}
|
Build model dictionary keyed by the relation's foreign key.
@param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
@return array<array<string, TRelatedModel>>
|
buildDictionary
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getPivotClass()
{
return $this->using ?? Pivot::class;
}
|
Get the class being used for pivot models.
@return class-string<TPivotModel>
|
getPivotClass
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function using($class)
{
$this->using = $class;
return $this;
}
|
Specify the custom pivot model to use for the relationship.
@template TNewPivotModel of \Illuminate\Database\Eloquent\Relations\Pivot
@param class-string<TNewPivotModel> $class
@return $this
@phpstan-this-out static<TRelatedModel, TDeclaringModel, TNewPivotModel, TAccessor>
|
using
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function as($accessor)
{
$this->accessor = $accessor;
return $this;
}
|
Specify the custom pivot accessor to use for the relationship.
@template TNewAccessor of string
@param TNewAccessor $accessor
@return $this
@phpstan-this-out static<TRelatedModel, TDeclaringModel, TPivotModel, TNewAccessor>
|
as
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivot($column, $operator = null, $value = null, $boolean = 'and')
{
$this->pivotWheres[] = func_get_args();
return $this->where($this->qualifyPivotColumn($column), $operator, $value, $boolean);
}
|
Set a where clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param mixed $operator
@param mixed $value
@param string $boolean
@return $this
|
wherePivot
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotBetween($column, array $values, $boolean = 'and', $not = false)
{
return $this->whereBetween($this->qualifyPivotColumn($column), $values, $boolean, $not);
}
|
Set a "where between" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param array $values
@param string $boolean
@param bool $not
@return $this
|
wherePivotBetween
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotBetween($column, array $values)
{
return $this->wherePivotBetween($column, $values, 'or');
}
|
Set a "or where between" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param array $values
@return $this
|
orWherePivotBetween
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotNotBetween($column, array $values, $boolean = 'and')
{
return $this->wherePivotBetween($column, $values, $boolean, true);
}
|
Set a "where pivot not between" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param array $values
@param string $boolean
@return $this
|
wherePivotNotBetween
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotNotBetween($column, array $values)
{
return $this->wherePivotBetween($column, $values, 'or', true);
}
|
Set a "or where not between" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param array $values
@return $this
|
orWherePivotNotBetween
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotIn($column, $values, $boolean = 'and', $not = false)
{
$this->pivotWhereIns[] = func_get_args();
return $this->whereIn($this->qualifyPivotColumn($column), $values, $boolean, $not);
}
|
Set a "where in" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param mixed $values
@param string $boolean
@param bool $not
@return $this
|
wherePivotIn
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivot($column, $operator = null, $value = null)
{
return $this->wherePivot($column, $operator, $value, 'or');
}
|
Set an "or where" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param mixed $operator
@param mixed $value
@return $this
|
orWherePivot
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function withPivotValue($column, $value = null)
{
if (is_array($column)) {
foreach ($column as $name => $value) {
$this->withPivotValue($name, $value);
}
return $this;
}
if (is_null($value)) {
throw new InvalidArgumentException('The provided value may not be null.');
}
$this->pivotValues[] = compact('column', 'value');
return $this->wherePivot($column, '=', $value);
}
|
Set a where clause for a pivot table column.
In addition, new pivot records will receive this value.
@param string|\Illuminate\Contracts\Database\Query\Expression|array<string, string> $column
@param mixed $value
@return $this
@throws \InvalidArgumentException
|
withPivotValue
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotIn($column, $values)
{
return $this->wherePivotIn($column, $values, 'or');
}
|
Set an "or where in" clause for a pivot table column.
@param string $column
@param mixed $values
@return $this
|
orWherePivotIn
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotNotIn($column, $values, $boolean = 'and')
{
return $this->wherePivotIn($column, $values, $boolean, true);
}
|
Set a "where not in" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param mixed $values
@param string $boolean
@return $this
|
wherePivotNotIn
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotNotIn($column, $values)
{
return $this->wherePivotNotIn($column, $values, 'or');
}
|
Set an "or where not in" clause for a pivot table column.
@param string $column
@param mixed $values
@return $this
|
orWherePivotNotIn
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotNull($column, $boolean = 'and', $not = false)
{
$this->pivotWhereNulls[] = func_get_args();
return $this->whereNull($this->qualifyPivotColumn($column), $boolean, $not);
}
|
Set a "where null" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param string $boolean
@param bool $not
@return $this
|
wherePivotNull
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function wherePivotNotNull($column, $boolean = 'and')
{
return $this->wherePivotNull($column, $boolean, true);
}
|
Set a "where not null" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param string $boolean
@return $this
|
wherePivotNotNull
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotNull($column, $not = false)
{
return $this->wherePivotNull($column, 'or', $not);
}
|
Set a "or where null" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param bool $not
@return $this
|
orWherePivotNull
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orWherePivotNotNull($column)
{
return $this->orWherePivotNull($column, true);
}
|
Set a "or where not null" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@return $this
|
orWherePivotNotNull
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orderByPivot($column, $direction = 'asc')
{
return $this->orderBy($this->qualifyPivotColumn($column), $direction);
}
|
Add an "order by" clause for a pivot table column.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@param string $direction
@return $this
|
orderByPivot
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function findOrNew($id, $columns = ['*'])
{
if (is_null($instance = $this->find($id, $columns))) {
$instance = $this->related->newInstance();
}
return $instance;
}
|
Find a related model by its primary key or return a new instance of the related model.
@param mixed $id
@param array $columns
@return (
$id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
: TRelatedModel&object{pivot: TPivotModel}
)
|
findOrNew
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function firstOrNew(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->related->where($attributes)->first())) {
$instance = $this->related->newInstance(array_merge($attributes, $values));
}
return $instance;
}
|
Get the first related model record matching the attributes or instantiate it.
@param array $attributes
@param array $values
@return TRelatedModel&object{pivot: TPivotModel}
|
firstOrNew
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function firstOrCreate(array $attributes = [], array $values = [], array $joining = [], $touch = true)
{
if (is_null($instance = (clone $this)->where($attributes)->first())) {
if (is_null($instance = $this->related->where($attributes)->first())) {
$instance = $this->createOrFirst($attributes, $values, $joining, $touch);
} else {
try {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
} catch (UniqueConstraintViolationException) {
// Nothing to do, the model was already attached...
}
}
}
return $instance;
}
|
Get the first record matching the attributes. If the record is not found, create it.
@param array $attributes
@param array $values
@param array $joining
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
firstOrCreate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function createOrFirst(array $attributes = [], array $values = [], array $joining = [], $touch = true)
{
try {
return $this->getQuery()->withSavePointIfNeeded(fn () => $this->create(array_merge($attributes, $values), $joining, $touch));
} catch (UniqueConstraintViolationException $e) {
// ...
}
try {
return tap($this->related->where($attributes)->first() ?? throw $e, function ($instance) use ($joining, $touch) {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
});
} catch (UniqueConstraintViolationException $e) {
return (clone $this)->useWritePdo()->where($attributes)->first() ?? throw $e;
}
}
|
Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record.
@param array $attributes
@param array $values
@param array $joining
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
createOrFirst
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
return tap($this->firstOrCreate($attributes, $values, $joining, $touch), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values);
$instance->save(['touch' => false]);
}
});
}
|
Create or update a related record matching the attributes, and fill it with values.
@param array $attributes
@param array $values
@param array $joining
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
updateOrCreate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function find($id, $columns = ['*'])
{
if (! $id instanceof Model && (is_array($id) || $id instanceof Arrayable)) {
return $this->findMany($id, $columns);
}
return $this->where(
$this->getRelated()->getQualifiedKeyName(), '=', $this->parseId($id)
)->first($columns);
}
|
Find a related model by its primary key.
@param mixed $id
@param array $columns
@return (
$id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
: (TRelatedModel&object{pivot: TPivotModel})|null
)
|
find
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function findSole($id, $columns = ['*'])
{
return $this->where(
$this->getRelated()->getQualifiedKeyName(), '=', $this->parseId($id)
)->sole($columns);
}
|
Find a sole related model by its primary key.
@param mixed $id
@param array $columns
@return TRelatedModel&object{pivot: TPivotModel}
@throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
@throws \Illuminate\Database\MultipleRecordsFoundException
|
findSole
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function findMany($ids, $columns = ['*'])
{
$ids = $ids instanceof Arrayable ? $ids->toArray() : $ids;
if (empty($ids)) {
return $this->getRelated()->newCollection();
}
return $this->whereKey(
$this->parseIds($ids)
)->get($columns);
}
|
Find multiple related models by their primary keys.
@param \Illuminate\Contracts\Support\Arrayable|array $ids
@param array $columns
@return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
|
findMany
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function findOrFail($id, $columns = ['*'])
{
$result = $this->find($id, $columns);
$id = $id instanceof Arrayable ? $id->toArray() : $id;
if (is_array($id)) {
if (count($result) === count(array_unique($id))) {
return $result;
}
} elseif (! is_null($result)) {
return $result;
}
throw (new ModelNotFoundException)->setModel(get_class($this->related), $id);
}
|
Find a related model by its primary key or throw an exception.
@param mixed $id
@param array $columns
@return (
$id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
: TRelatedModel&object{pivot: TPivotModel}
)
@throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
|
findOrFail
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function findOr($id, $columns = ['*'], ?Closure $callback = null)
{
if ($columns instanceof Closure) {
$callback = $columns;
$columns = ['*'];
}
$result = $this->find($id, $columns);
$id = $id instanceof Arrayable ? $id->toArray() : $id;
if (is_array($id)) {
if (count($result) === count(array_unique($id))) {
return $result;
}
} elseif (! is_null($result)) {
return $result;
}
return $callback();
}
|
Find a related model by its primary key or call a callback.
@template TValue
@param mixed $id
@param (\Closure(): TValue)|list<string>|string $columns
@param (\Closure(): TValue)|null $callback
@return (
$id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>|TValue
: (TRelatedModel&object{pivot: TPivotModel})|TValue
)
|
findOr
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
{
return $this->where($column, $operator, $value, $boolean)->first();
}
|
Add a basic where clause to the query, and return the first result.
@param \Closure|string|array $column
@param mixed $operator
@param mixed $value
@param string $boolean
@return (TRelatedModel&object{pivot: TPivotModel})|null
|
firstWhere
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function first($columns = ['*'])
{
$results = $this->take(1)->get($columns);
return count($results) > 0 ? $results->first() : null;
}
|
Execute the query and get the first result.
@param array $columns
@return (TRelatedModel&object{pivot: TPivotModel})|null
|
first
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function firstOrFail($columns = ['*'])
{
if (! is_null($model = $this->first($columns))) {
return $model;
}
throw (new ModelNotFoundException)->setModel(get_class($this->related));
}
|
Execute the query and get the first result or throw an exception.
@param array $columns
@return TRelatedModel&object{pivot: TPivotModel}
@throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
|
firstOrFail
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function firstOr($columns = ['*'], ?Closure $callback = null)
{
if ($columns instanceof Closure) {
$callback = $columns;
$columns = ['*'];
}
if (! is_null($model = $this->first($columns))) {
return $model;
}
return $callback();
}
|
Execute the query and get the first result or call a callback.
@template TValue
@param (\Closure(): TValue)|list<string> $columns
@param (\Closure(): TValue)|null $callback
@return (TRelatedModel&object{pivot: TPivotModel})|TValue
|
firstOr
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function shouldSelect(array $columns = ['*'])
{
if ($columns == ['*']) {
$columns = [$this->related->qualifyColumn('*')];
}
return array_merge($columns, $this->aliasedPivotColumns());
}
|
Get the select columns for the relation query.
@param array $columns
@return array
|
shouldSelect
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function aliasedPivotColumns()
{
return (new BaseCollection([
$this->foreignPivotKey,
$this->relatedPivotKey,
...$this->pivotColumns,
]))
->map(fn ($column) => $this->qualifyPivotColumn($column).' as pivot_'.$column)
->unique()
->all();
}
|
Get the pivot columns for the relation.
"pivot_" is prefixed at each column for easy removal later.
@return array
|
aliasedPivotColumns
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$this->query->addSelect($this->shouldSelect($columns));
return tap($this->query->paginate($perPage, $columns, $pageName, $page), function ($paginator) {
$this->hydratePivotRelation($paginator->items());
});
}
|
Get a paginator for the "select" statement.
@param int|null $perPage
@param array $columns
@param string $pageName
@param int|null $page
@return \Illuminate\Pagination\LengthAwarePaginator<int, TRelatedModel&object{pivot: TPivotModel}>
|
paginate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$this->query->addSelect($this->shouldSelect($columns));
return tap($this->query->simplePaginate($perPage, $columns, $pageName, $page), function ($paginator) {
$this->hydratePivotRelation($paginator->items());
});
}
|
Paginate the given query into a simple paginator.
@param int|null $perPage
@param array $columns
@param string $pageName
@param int|null $page
@return \Illuminate\Contracts\Pagination\Paginator<int, TRelatedModel&object{pivot: TPivotModel}>
|
simplePaginate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
{
$this->query->addSelect($this->shouldSelect($columns));
return tap($this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor), function ($paginator) {
$this->hydratePivotRelation($paginator->items());
});
}
|
Paginate the given query into a cursor paginator.
@param int|null $perPage
@param array $columns
@param string $cursorName
@param string|null $cursor
@return \Illuminate\Contracts\Pagination\CursorPaginator<int, TRelatedModel&object{pivot: TPivotModel}>
|
cursorPaginate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function chunk($count, callable $callback)
{
return $this->prepareQueryBuilder()->chunk($count, function ($results, $page) use ($callback) {
$this->hydratePivotRelation($results->all());
return $callback($results, $page);
});
}
|
Chunk the results of the query.
@param int $count
@param callable $callback
@return bool
|
chunk
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
return $this->orderedChunkById($count, $callback, $column, $alias);
}
|
Chunk the results of a query by comparing numeric IDs.
@param int $count
@param callable $callback
@param string|null $column
@param string|null $alias
@return bool
|
chunkById
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function chunkByIdDesc($count, callable $callback, $column = null, $alias = null)
{
return $this->orderedChunkById($count, $callback, $column, $alias, descending: true);
}
|
Chunk the results of a query by comparing IDs in descending order.
@param int $count
@param callable $callback
@param string|null $column
@param string|null $alias
@return bool
|
chunkByIdDesc
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function eachById(callable $callback, $count = 1000, $column = null, $alias = null)
{
return $this->chunkById($count, function ($results, $page) use ($callback, $count) {
foreach ($results as $key => $value) {
if ($callback($value, (($page - 1) * $count) + $key) === false) {
return false;
}
}
}, $column, $alias);
}
|
Execute a callback over each item while chunking by ID.
@param callable $callback
@param int $count
@param string|null $column
@param string|null $alias
@return bool
|
eachById
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function orderedChunkById($count, callable $callback, $column = null, $alias = null, $descending = false)
{
$column ??= $this->getRelated()->qualifyColumn(
$this->getRelatedKeyName()
);
$alias ??= $this->getRelatedKeyName();
return $this->prepareQueryBuilder()->orderedChunkById($count, function ($results, $page) use ($callback) {
$this->hydratePivotRelation($results->all());
return $callback($results, $page);
}, $column, $alias, $descending);
}
|
Chunk the results of a query by comparing IDs in a given order.
@param int $count
@param callable $callback
@param string|null $column
@param string|null $alias
@param bool $descending
@return bool
|
orderedChunkById
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function each(callable $callback, $count = 1000)
{
return $this->chunk($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($value, $key) === false) {
return false;
}
}
});
}
|
Execute a callback over each item while chunking.
@param callable $callback
@param int $count
@return bool
|
each
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function lazy($chunkSize = 1000)
{
return $this->prepareQueryBuilder()->lazy($chunkSize)->map(function ($model) {
$this->hydratePivotRelation([$model]);
return $model;
});
}
|
Query lazily, by chunks of the given size.
@param int $chunkSize
@return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel}>
|
lazy
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function lazyById($chunkSize = 1000, $column = null, $alias = null)
{
$column ??= $this->getRelated()->qualifyColumn(
$this->getRelatedKeyName()
);
$alias ??= $this->getRelatedKeyName();
return $this->prepareQueryBuilder()->lazyById($chunkSize, $column, $alias)->map(function ($model) {
$this->hydratePivotRelation([$model]);
return $model;
});
}
|
Query lazily, by chunking the results of a query by comparing IDs.
@param int $chunkSize
@param string|null $column
@param string|null $alias
@return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel}>
|
lazyById
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function lazyByIdDesc($chunkSize = 1000, $column = null, $alias = null)
{
$column ??= $this->getRelated()->qualifyColumn(
$this->getRelatedKeyName()
);
$alias ??= $this->getRelatedKeyName();
return $this->prepareQueryBuilder()->lazyByIdDesc($chunkSize, $column, $alias)->map(function ($model) {
$this->hydratePivotRelation([$model]);
return $model;
});
}
|
Query lazily, by chunking the results of a query by comparing IDs in descending order.
@param int $chunkSize
@param string|null $column
@param string|null $alias
@return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel}>
|
lazyByIdDesc
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function cursor()
{
return $this->prepareQueryBuilder()->cursor()->map(function ($model) {
$this->hydratePivotRelation([$model]);
return $model;
});
}
|
Get a lazy collection for the given query.
@return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel}>
|
cursor
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function prepareQueryBuilder()
{
return $this->query->addSelect($this->shouldSelect());
}
|
Prepare the query builder for query execution.
@return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
|
prepareQueryBuilder
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function hydratePivotRelation(array $models)
{
// To hydrate the pivot relationship, we will just gather the pivot attributes
// and create a new Pivot model, which is basically a dynamic model that we
// will set the attributes, table, and connections on it so it will work.
foreach ($models as $model) {
$model->setRelation($this->accessor, $this->newExistingPivot(
$this->migratePivotAttributes($model)
));
}
}
|
Hydrate the pivot table relationship on the models.
@param array<int, TRelatedModel> $models
@return void
|
hydratePivotRelation
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function migratePivotAttributes(Model $model)
{
$values = [];
foreach ($model->getAttributes() as $key => $value) {
// To get the pivots attributes we will just take any of the attributes which
// begin with "pivot_" and add those to this arrays, as well as unsetting
// them from the parent's models since they exist in a different table.
if (str_starts_with($key, 'pivot_')) {
$values[substr($key, 6)] = $value;
unset($model->$key);
}
}
return $values;
}
|
Get the pivot attributes from a model.
@param TRelatedModel $model
@return array
|
migratePivotAttributes
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function touchIfTouching()
{
if ($this->touchingParent()) {
$this->getParent()->touch();
}
if ($this->getParent()->touches($this->relationName)) {
$this->touch();
}
}
|
If we're touching the parent model, touch.
@return void
|
touchIfTouching
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function touchingParent()
{
return $this->getRelated()->touches($this->guessInverseRelation());
}
|
Determine if we should touch the parent on sync.
@return bool
|
touchingParent
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
protected function guessInverseRelation()
{
return Str::camel(Str::pluralStudly(class_basename($this->getParent())));
}
|
Attempt to guess the name of the inverse of the relation.
@return string
|
guessInverseRelation
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function touch()
{
if ($this->related->isIgnoringTouch()) {
return;
}
$columns = [
$this->related->getUpdatedAtColumn() => $this->related->freshTimestampString(),
];
// If we actually have IDs for the relation, we will run the query to update all
// the related model's timestamps, to make sure these all reflect the changes
// to the parent models. This will help us keep any caching synced up here.
if (count($ids = $this->allRelatedIds()) > 0) {
$this->getRelated()->newQueryWithoutRelationships()->whereKey($ids)->update($columns);
}
}
|
Touch all of the related models for the relationship.
E.g.: Touch all roles associated with this user.
@return void
|
touch
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function allRelatedIds()
{
return $this->newPivotQuery()->pluck($this->relatedPivotKey);
}
|
Get all of the IDs for the related models.
@return \Illuminate\Support\Collection<int, int|string>
|
allRelatedIds
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function save(Model $model, array $pivotAttributes = [], $touch = true)
{
$model->save(['touch' => false]);
$this->attach($model, $pivotAttributes, $touch);
return $model;
}
|
Save a new model and attach it to the parent model.
@param TRelatedModel $model
@param array $pivotAttributes
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
save
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = true)
{
return Model::withoutEvents(function () use ($model, $pivotAttributes, $touch) {
return $this->save($model, $pivotAttributes, $touch);
});
}
|
Save a new model without raising any events and attach it to the parent model.
@param TRelatedModel $model
@param array $pivotAttributes
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
saveQuietly
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function saveMany($models, array $pivotAttributes = [])
{
foreach ($models as $key => $model) {
$this->save($model, (array) ($pivotAttributes[$key] ?? []), false);
}
$this->touchIfTouching();
return $models;
}
|
Save an array of new models and attach them to the parent model.
@template TContainer of \Illuminate\Support\Collection<array-key, TRelatedModel>|array<array-key, TRelatedModel>
@param TContainer $models
@param array $pivotAttributes
@return TContainer
|
saveMany
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function saveManyQuietly($models, array $pivotAttributes = [])
{
return Model::withoutEvents(function () use ($models, $pivotAttributes) {
return $this->saveMany($models, $pivotAttributes);
});
}
|
Save an array of new models without raising any events and attach them to the parent model.
@template TContainer of \Illuminate\Support\Collection<array-key, TRelatedModel>|array<array-key, TRelatedModel>
@param TContainer $models
@param array $pivotAttributes
@return TContainer
|
saveManyQuietly
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function create(array $attributes = [], array $joining = [], $touch = true)
{
$attributes = array_merge($this->getQuery()->pendingAttributes, $attributes);
$instance = $this->related->newInstance($attributes);
// Once we save the related model, we need to attach it to the base model via
// through intermediate table so we'll use the existing "attach" method to
// accomplish this which will insert the record and any more attributes.
$instance->save(['touch' => false]);
$this->attach($instance, $joining, $touch);
return $instance;
}
|
Create a new instance of the related model.
@param array $attributes
@param array $joining
@param bool $touch
@return TRelatedModel&object{pivot: TPivotModel}
|
create
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function createMany(iterable $records, array $joinings = [])
{
$instances = [];
foreach ($records as $key => $record) {
$instances[] = $this->create($record, (array) ($joinings[$key] ?? []), false);
}
$this->touchIfTouching();
return $instances;
}
|
Create an array of new instances of the related models.
@param iterable $records
@param array $joinings
@return array<int, TRelatedModel&object{pivot: TPivotModel}>
|
createMany
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getRelationExistenceQueryForSelfJoin(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$query->select($columns);
$query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash());
$this->related->setTable($hash);
$this->performJoin($query);
return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
}
|
Add the constraints for a relationship query on the same table.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param \Illuminate\Database\Eloquent\Builder<TDeclaringModel> $parentQuery
@param array|mixed $columns
@return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
|
getRelationExistenceQueryForSelfJoin
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getExistenceCompareKey()
{
return $this->getQualifiedForeignPivotKeyName();
}
|
Get the key for comparing against the parent key in "has" query.
@return string
|
getExistenceCompareKey
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function withTimestamps($createdAt = null, $updatedAt = null)
{
$this->withTimestamps = true;
$this->pivotCreatedAt = $createdAt;
$this->pivotUpdatedAt = $updatedAt;
return $this->withPivot($this->createdAt(), $this->updatedAt());
}
|
Specify that the pivot table has creation and update timestamps.
@param mixed $createdAt
@param mixed $updatedAt
@return $this
|
withTimestamps
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function createdAt()
{
return $this->pivotCreatedAt ?? $this->parent->getCreatedAtColumn() ?? Model::CREATED_AT;
}
|
Get the name of the "created at" column.
@return string
|
createdAt
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function updatedAt()
{
return $this->pivotUpdatedAt ?? $this->parent->getUpdatedAtColumn() ?? Model::UPDATED_AT;
}
|
Get the name of the "updated at" column.
@return string
|
updatedAt
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getForeignPivotKeyName()
{
return $this->foreignPivotKey;
}
|
Get the foreign key for the relation.
@return string
|
getForeignPivotKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getQualifiedForeignPivotKeyName()
{
return $this->qualifyPivotColumn($this->foreignPivotKey);
}
|
Get the fully qualified foreign key for the relation.
@return string
|
getQualifiedForeignPivotKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getRelatedPivotKeyName()
{
return $this->relatedPivotKey;
}
|
Get the "related key" for the relation.
@return string
|
getRelatedPivotKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getQualifiedRelatedPivotKeyName()
{
return $this->qualifyPivotColumn($this->relatedPivotKey);
}
|
Get the fully qualified "related key" for the relation.
@return string
|
getQualifiedRelatedPivotKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getParentKeyName()
{
return $this->parentKey;
}
|
Get the parent key for the relationship.
@return string
|
getParentKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getQualifiedParentKeyName()
{
return $this->parent->qualifyColumn($this->parentKey);
}
|
Get the fully qualified parent key name for the relation.
@return string
|
getQualifiedParentKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getRelatedKeyName()
{
return $this->relatedKey;
}
|
Get the related key for the relationship.
@return string
|
getRelatedKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getQualifiedRelatedKeyName()
{
return $this->related->qualifyColumn($this->relatedKey);
}
|
Get the fully qualified related key name for the relation.
@return string
|
getQualifiedRelatedKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getTable()
{
return $this->table;
}
|
Get the intermediate table for the relationship.
@return string
|
getTable
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getRelationName()
{
return $this->relationName;
}
|
Get the relationship name for the relationship.
@return string
|
getRelationName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getPivotAccessor()
{
return $this->accessor;
}
|
Get the name of the pivot accessor for this relationship.
@return TAccessor
|
getPivotAccessor
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function getPivotColumns()
{
return $this->pivotColumns;
}
|
Get the pivot columns for this relationship.
@return array
|
getPivotColumns
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function qualifyPivotColumn($column)
{
if ($this->query->getQuery()->getGrammar()->isExpression($column)) {
return $column;
}
return str_contains($column, '.')
? $column
: $this->table.'.'.$column;
}
|
Qualify the given column name by the pivot table.
@param string|\Illuminate\Contracts\Database\Query\Expression $column
@return string|\Illuminate\Contracts\Database\Query\Expression
|
qualifyPivotColumn
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsToMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsToMany.php
|
MIT
|
public function one()
{
return HasOne::noConstraints(fn () => tap(
new HasOne(
$this->getQuery(),
$this->parent,
$this->foreignKey,
$this->localKey
),
function ($hasOne) {
if ($inverse = $this->getInverseRelationship()) {
$hasOne->inverse($inverse);
}
}
));
}
|
Convert the relationship to a "has one" relationship.
@return \Illuminate\Database\Eloquent\Relations\HasOne<TRelatedModel, TDeclaringModel>
|
one
|
php
|
illuminate/database
|
Eloquent/Relations/HasMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasMany.php
|
MIT
|
public function one()
{
return HasOneThrough::noConstraints(fn () => new HasOneThrough(
tap($this->getQuery(), fn (Builder $query) => $query->getQuery()->joins = []),
$this->farParent,
$this->throughParent,
$this->getFirstKeyName(),
$this->getForeignKeyName(),
$this->getLocalKeyName(),
$this->getSecondLocalKeyName(),
));
}
|
Convert the relationship to a "has one through" relationship.
@return \Illuminate\Database\Eloquent\Relations\HasOneThrough<TRelatedModel, TIntermediateModel, TDeclaringModel>
|
one
|
php
|
illuminate/database
|
Eloquent/Relations/HasManyThrough.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasManyThrough.php
|
MIT
|
public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null)
{
$query->addSelect($this->foreignKey);
}
|
Add constraints for inner join subselect for one of many relationships.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param string|null $column
@param string|null $aggregate
@return void
|
addOneOfManySubQueryConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/HasOne.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOne.php
|
MIT
|
public function getOneOfManySubQuerySelectColumns()
{
return $this->foreignKey;
}
|
Get the columns that should be selected by the one of many subquery.
@return array|string
|
getOneOfManySubQuerySelectColumns
|
php
|
illuminate/database
|
Eloquent/Relations/HasOne.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOne.php
|
MIT
|
public function addOneOfManyJoinSubQueryConstraints(JoinClause $join)
{
$join->on($this->qualifySubSelectColumn($this->foreignKey), '=', $this->qualifyRelatedColumn($this->foreignKey));
}
|
Add join query constraints for one of many relationships.
@param \Illuminate\Database\Query\JoinClause $join
@return void
|
addOneOfManyJoinSubQueryConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/HasOne.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOne.php
|
MIT
|
public function newRelatedInstanceFor(Model $parent)
{
return tap($this->related->newInstance(), function ($instance) use ($parent) {
$instance->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey});
$this->applyInverseRelationToModel($instance, $parent);
});
}
|
Make a new related instance for the given model.
@param TDeclaringModel $parent
@return TRelatedModel
|
newRelatedInstanceFor
|
php
|
illuminate/database
|
Eloquent/Relations/HasOne.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOne.php
|
MIT
|
protected function getRelatedKeyFrom(Model $model)
{
return $model->getAttribute($this->getForeignKeyName());
}
|
Get the value of the model's foreign key.
@param TRelatedModel $model
@return int|string
|
getRelatedKeyFrom
|
php
|
illuminate/database
|
Eloquent/Relations/HasOne.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOne.php
|
MIT
|
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
{
$this->localKey = $localKey;
$this->foreignKey = $foreignKey;
parent::__construct($query, $parent);
}
|
Create a new has one or many relationship instance.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param TDeclaringModel $parent
@param string $foreignKey
@param string $localKey
|
__construct
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
public function make(array $attributes = [])
{
return tap($this->related->newInstance($attributes), function ($instance) {
$this->setForeignAttributesForCreate($instance);
$this->applyInverseRelationToModel($instance);
});
}
|
Create and return an un-saved instance of the related model.
@param array $attributes
@return TRelatedModel
|
make
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
public function makeMany($records)
{
$instances = $this->related->newCollection();
foreach ($records as $record) {
$instances->push($this->make($record));
}
return $instances;
}
|
Create and return an un-saved instance of the related models.
@param iterable $records
@return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
|
makeMany
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
public function addConstraints()
{
if (static::$constraints) {
$query = $this->getRelationQuery();
$query->where($this->foreignKey, '=', $this->getParentKey());
$query->whereNotNull($this->foreignKey);
}
}
|
Set the base constraints on the relation query.
@return void
|
addConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
public function matchOne(array $models, EloquentCollection $results, $relation)
{
return $this->matchOneOrMany($models, $results, $relation, 'one');
}
|
Match the eagerly loaded results to their single parents.
@param array<int, TDeclaringModel> $models
@param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
@param string $relation
@return array<int, TDeclaringModel>
|
matchOne
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
public function matchMany(array $models, EloquentCollection $results, $relation)
{
return $this->matchOneOrMany($models, $results, $relation, 'many');
}
|
Match the eagerly loaded results to their many parents.
@param array<int, TDeclaringModel> $models
@param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
@param string $relation
@return array<int, TDeclaringModel>
|
matchMany
|
php
|
illuminate/database
|
Eloquent/Relations/HasOneOrMany.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/HasOneOrMany.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.