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 joiningTableSegment() { return Str::snake(class_basename($this)); }
Get this model's half of the intermediate table name for belongsToMany relationships. @return string
joiningTableSegment
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function touches($relation) { return in_array($relation, $this->getTouchedRelations()); }
Determine if the model touches a given relation. @param string $relation @return bool
touches
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function touchOwners() { $this->withoutRecursion(function () { foreach ($this->getTouchedRelations() as $relation) { $this->$relation()->touch(); if ($this->$relation instanceof self) { $this->$relation->fireModelEvent('saved', false); $this->$relation->touchOwners(); } elseif ($this->$relation instanceof EloquentCollection) { $this->$relation->each->touchOwners(); } } }); }
Touch the owning relations of the model. @return void
touchOwners
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function getMorphs($name, $type, $id) { return [$type ?: $name.'_type', $id ?: $name.'_id']; }
Get the polymorphic relationship columns. @param string $name @param string $type @param string $id @return array
getMorphs
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function getMorphClass() { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array(static::class, $morphMap)) { return array_search(static::class, $morphMap, true); } if (static::class === Pivot::class) { return static::class; } if (Relation::requiresMorphMap()) { throw new ClassMorphViolationException($this); } return static::class; }
Get the class name for polymorphic relations. @return string
getMorphClass
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newRelatedInstance($class) { return tap(new $class, function ($instance) { if (! $instance->getConnectionName()) { $instance->setConnection($this->connection); } }); }
Create a new model instance for a related model. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $class @return TRelatedModel
newRelatedInstance
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newRelatedThroughInstance($class) { return new $class; }
Create a new model instance for a related "through" model. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $class @return TRelatedModel
newRelatedThroughInstance
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function getRelations() { return $this->relations; }
Get all the loaded relations for the instance. @return array
getRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function getRelation($relation) { return $this->relations[$relation]; }
Get a specified relationship. @param string $relation @return mixed
getRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function relationLoaded($key) { return array_key_exists($key, $this->relations); }
Determine if the given relation is loaded. @param string $key @return bool
relationLoaded
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function setRelation($relation, $value) { $this->relations[$relation] = $value; $this->propagateRelationAutoloadCallbackToRelation($relation, $value); return $this; }
Set the given relationship on the model. @param string $relation @param mixed $value @return $this
setRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function unsetRelation($relation) { unset($this->relations[$relation]); return $this; }
Unset a loaded relationship. @param string $relation @return $this
unsetRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function setRelations(array $relations) { $this->relations = $relations; return $this; }
Set the entire relations array on the model. @param array $relations @return $this
setRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function withRelationshipAutoloading() { $this->newCollection([$this])->withRelationshipAutoloading(); return $this; }
Enable relationship autoloading for this model. @return $this
withRelationshipAutoloading
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function withoutRelations() { $model = clone $this; return $model->unsetRelations(); }
Duplicate the instance and unset all the loaded relations. @return $this
withoutRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function unsetRelations() { $this->relations = []; return $this; }
Unset all the loaded relations for the instance. @return $this
unsetRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function getTouchedRelations() { return $this->touches; }
Get the relationships that are touched on save. @return array
getTouchedRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function setTouchedRelations(array $touches) { $this->touches = $touches; return $this; }
Set the relationships that are touched on save. @param array $touches @return $this
setTouchedRelations
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function touch($attribute = null) { if ($attribute) { $this->$attribute = $this->freshTimestamp(); return $this->save(); } if (! $this->usesTimestamps()) { return false; } $this->updateTimestamps(); return $this->save(); }
Update the model's update timestamp. @param string|null $attribute @return bool
touch
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function touchQuietly($attribute = null) { return static::withoutEvents(fn () => $this->touch($attribute)); }
Update the model's update timestamp without raising any events. @param string|null $attribute @return bool
touchQuietly
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function updateTimestamps() { $time = $this->freshTimestamp(); $updatedAtColumn = $this->getUpdatedAtColumn(); if (! is_null($updatedAtColumn) && ! $this->isDirty($updatedAtColumn)) { $this->setUpdatedAt($time); } $createdAtColumn = $this->getCreatedAtColumn(); if (! $this->exists && ! is_null($createdAtColumn) && ! $this->isDirty($createdAtColumn)) { $this->setCreatedAt($time); } return $this; }
Update the creation and update timestamps. @return $this
updateTimestamps
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function setCreatedAt($value) { $this->{$this->getCreatedAtColumn()} = $value; return $this; }
Set the value of the "created at" attribute. @param mixed $value @return $this
setCreatedAt
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function setUpdatedAt($value) { $this->{$this->getUpdatedAtColumn()} = $value; return $this; }
Set the value of the "updated at" attribute. @param mixed $value @return $this
setUpdatedAt
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function freshTimestamp() { return Date::now(); }
Get a fresh timestamp for the model. @return \Illuminate\Support\Carbon
freshTimestamp
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function freshTimestampString() { return $this->fromDateTime($this->freshTimestamp()); }
Get a fresh timestamp for the model. @return string
freshTimestampString
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function usesTimestamps() { return $this->timestamps && ! static::isIgnoringTimestamps($this::class); }
Determine if the model uses timestamps. @return bool
usesTimestamps
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function getCreatedAtColumn() { return static::CREATED_AT; }
Get the name of the "created at" column. @return string|null
getCreatedAtColumn
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function getUpdatedAtColumn() { return static::UPDATED_AT; }
Get the name of the "updated at" column. @return string|null
getUpdatedAtColumn
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function getQualifiedCreatedAtColumn() { $column = $this->getCreatedAtColumn(); return $column ? $this->qualifyColumn($column) : null; }
Get the fully qualified "created at" column. @return string|null
getQualifiedCreatedAtColumn
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function getQualifiedUpdatedAtColumn() { $column = $this->getUpdatedAtColumn(); return $column ? $this->qualifyColumn($column) : null; }
Get the fully qualified "updated at" column. @return string|null
getQualifiedUpdatedAtColumn
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public static function withoutTimestamps(callable $callback) { return static::withoutTimestampsOn([static::class], $callback); }
Disable timestamps for the current class during the given callback scope. @param callable $callback @return mixed
withoutTimestamps
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public static function withoutTimestampsOn($models, $callback) { static::$ignoreTimestampsOn = array_values(array_merge(static::$ignoreTimestampsOn, $models)); try { return $callback(); } finally { foreach ($models as $model) { if (($key = array_search($model, static::$ignoreTimestampsOn, true)) !== false) { unset(static::$ignoreTimestampsOn[$key]); } } } }
Disable timestamps for the given model classes during the given callback scope. @param array $models @param callable $callback @return mixed
withoutTimestampsOn
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public static function isIgnoringTimestamps($class = null) { $class ??= static::class; foreach (static::$ignoreTimestampsOn as $ignoredClass) { if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) { return true; } } return false; }
Determine if the given model is ignoring timestamps / touches. @param string|null $class @return bool
isIgnoringTimestamps
php
illuminate/database
Eloquent/Concerns/HasTimestamps.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasTimestamps.php
MIT
public function newUniqueId() { return strtolower((string) Str::ulid()); }
Generate a new unique key for the model. @return string
newUniqueId
php
illuminate/database
Eloquent/Concerns/HasUlids.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUlids.php
MIT
protected function isValidUniqueId($value): bool { return Str::isUlid($value); }
Determine if given key is valid. @param mixed $value @return bool
isValidUniqueId
php
illuminate/database
Eloquent/Concerns/HasUlids.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUlids.php
MIT
public function usesUniqueIds() { return $this->usesUniqueIds; }
Determine if the model uses unique ids. @return bool
usesUniqueIds
php
illuminate/database
Eloquent/Concerns/HasUniqueIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueIds.php
MIT
public function setUniqueIds() { foreach ($this->uniqueIds() as $column) { if (empty($this->{$column})) { $this->{$column} = $this->newUniqueId(); } } }
Generate unique keys for the model. @return void
setUniqueIds
php
illuminate/database
Eloquent/Concerns/HasUniqueIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueIds.php
MIT
public function newUniqueId() { return null; }
Generate a new key for the model. @return string
newUniqueId
php
illuminate/database
Eloquent/Concerns/HasUniqueIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueIds.php
MIT
public function uniqueIds() { return []; }
Get the columns that should receive a unique identifier. @return array
uniqueIds
php
illuminate/database
Eloquent/Concerns/HasUniqueIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueIds.php
MIT
public function uniqueIds() { return $this->usesUniqueIds() ? [$this->getKeyName()] : parent::uniqueIds(); }
Get the columns that should receive a unique identifier. @return array
uniqueIds
php
illuminate/database
Eloquent/Concerns/HasUniqueStringIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueStringIds.php
MIT
public function resolveRouteBindingQuery($query, $value, $field = null) { if ($field && in_array($field, $this->uniqueIds()) && ! $this->isValidUniqueId($value)) { $this->handleInvalidUniqueId($value, $field); } if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! $this->isValidUniqueId($value)) { $this->handleInvalidUniqueId($value, $field); } return parent::resolveRouteBindingQuery($query, $value, $field); }
Retrieve the model for a bound value. @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query @param mixed $value @param string|null $field @return \Illuminate\Contracts\Database\Eloquent\Builder @throws \Illuminate\Database\Eloquent\ModelNotFoundException
resolveRouteBindingQuery
php
illuminate/database
Eloquent/Concerns/HasUniqueStringIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueStringIds.php
MIT
public function getKeyType() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return 'string'; } return parent::getKeyType(); }
Get the auto-incrementing key type. @return string
getKeyType
php
illuminate/database
Eloquent/Concerns/HasUniqueStringIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueStringIds.php
MIT
public function getIncrementing() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return false; } return parent::getIncrementing(); }
Get the value indicating whether the IDs are incrementing. @return bool
getIncrementing
php
illuminate/database
Eloquent/Concerns/HasUniqueStringIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueStringIds.php
MIT
protected function handleInvalidUniqueId($value, $field) { throw (new ModelNotFoundException)->setModel(get_class($this), $value); }
Throw an exception for the given invalid unique ID. @param mixed $value @param string|null $field @return never @throws \Illuminate\Database\Eloquent\ModelNotFoundException
handleInvalidUniqueId
php
illuminate/database
Eloquent/Concerns/HasUniqueStringIds.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUniqueStringIds.php
MIT
public function newUniqueId() { return (string) Str::uuid7(); }
Generate a new unique key for the model. @return string
newUniqueId
php
illuminate/database
Eloquent/Concerns/HasUuids.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUuids.php
MIT
protected function isValidUniqueId($value): bool { return Str::isUuid($value); }
Determine if given key is valid. @param mixed $value @return bool
isValidUniqueId
php
illuminate/database
Eloquent/Concerns/HasUuids.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasUuids.php
MIT
public function newUniqueId() { return (string) Str::orderedUuid(); }
Generate a new UUID (version 4) for the model. @return string
newUniqueId
php
illuminate/database
Eloquent/Concerns/HasVersion4Uuids.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasVersion4Uuids.php
MIT
public function getHidden() { return $this->hidden; }
Get the hidden attributes for the model. @return array<string>
getHidden
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function setHidden(array $hidden) { $this->hidden = $hidden; return $this; }
Set the hidden attributes for the model. @param array<string> $hidden @return $this
setHidden
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function getVisible() { return $this->visible; }
Get the visible attributes for the model. @return array<string>
getVisible
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function setVisible(array $visible) { $this->visible = $visible; return $this; }
Set the visible attributes for the model. @param array<string> $visible @return $this
setVisible
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function makeVisible($attributes) { $attributes = is_array($attributes) ? $attributes : func_get_args(); $this->hidden = array_diff($this->hidden, $attributes); if (! empty($this->visible)) { $this->visible = array_values(array_unique(array_merge($this->visible, $attributes))); } return $this; }
Make the given, typically hidden, attributes visible. @param array<string>|string|null $attributes @return $this
makeVisible
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function makeVisibleIf($condition, $attributes) { return value($condition, $this) ? $this->makeVisible($attributes) : $this; }
Make the given, typically hidden, attributes visible if the given truth test passes. @param bool|\Closure $condition @param array<string>|string|null $attributes @return $this
makeVisibleIf
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function makeHidden($attributes) { $this->hidden = array_values(array_unique(array_merge( $this->hidden, is_array($attributes) ? $attributes : func_get_args() ))); return $this; }
Make the given, typically visible, attributes hidden. @param array<string>|string|null $attributes @return $this
makeHidden
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
public function makeHiddenIf($condition, $attributes) { return value($condition, $this) ? $this->makeHidden($attributes) : $this; }
Make the given, typically visible, attributes hidden if the given truth test passes. @param bool|\Closure $condition @param array<string>|string|null $attributes @return $this
makeHiddenIf
php
illuminate/database
Eloquent/Concerns/HidesAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HidesAttributes.php
MIT
protected function withoutRecursion($callback, $default = null) { $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); $onceable = Onceable::tryFromTrace($trace, $callback); if (is_null($onceable)) { return call_user_func($callback); } $stack = static::getRecursiveCallStack($this); if (array_key_exists($onceable->hash, $stack)) { return is_callable($stack[$onceable->hash]) ? static::setRecursiveCallValue($this, $onceable->hash, call_user_func($stack[$onceable->hash])) : $stack[$onceable->hash]; } try { static::setRecursiveCallValue($this, $onceable->hash, $default); return call_user_func($onceable->callable); } finally { static::clearRecursiveCallValue($this, $onceable->hash); } }
Prevent a method from being called multiple times on the same object within the same call stack. @param callable $callback @param mixed $default @return mixed
withoutRecursion
php
illuminate/database
Eloquent/Concerns/PreventsCircularRecursion.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/PreventsCircularRecursion.php
MIT
protected static function clearRecursiveCallValue($object, string $hash) { if ($stack = Arr::except(static::getRecursiveCallStack($object), $hash)) { static::getRecursionCache()->offsetSet($object, $stack); } elseif (static::getRecursionCache()->offsetExists($object)) { static::getRecursionCache()->offsetUnset($object); } }
Remove an entry from the recursion cache for an object. @param object $object @param string $hash
clearRecursiveCallValue
php
illuminate/database
Eloquent/Concerns/PreventsCircularRecursion.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/PreventsCircularRecursion.php
MIT
protected static function getRecursiveCallStack($object): array { return static::getRecursionCache()->offsetExists($object) ? static::getRecursionCache()->offsetGet($object) : []; }
Get the stack of methods being called recursively for the current object. @param object $object @return array
getRecursiveCallStack
php
illuminate/database
Eloquent/Concerns/PreventsCircularRecursion.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/PreventsCircularRecursion.php
MIT
protected static function getRecursionCache() { return static::$recursionCache ??= new WeakMap(); }
Get the current recursion cache being used by the model. @return WeakMap
getRecursionCache
php
illuminate/database
Eloquent/Concerns/PreventsCircularRecursion.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/PreventsCircularRecursion.php
MIT
protected static function setRecursiveCallValue($object, string $hash, $value) { static::getRecursionCache()->offsetSet( $object, tap(static::getRecursiveCallStack($object), fn (&$stack) => $stack[$hash] = $value), ); return static::getRecursiveCallStack($object)[$hash]; }
Set a value in the recursion cache for the given object and method. @param object $object @param string $hash @param mixed $value @return mixed
setRecursiveCallValue
php
illuminate/database
Eloquent/Concerns/PreventsCircularRecursion.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/PreventsCircularRecursion.php
MIT
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null) { if (is_string($relation)) { if (str_contains($relation, '.')) { return $this->hasNested($relation, $operator, $count, $boolean, $callback); } $relation = $this->getRelationWithoutConstraints($relation); } if ($relation instanceof MorphTo) { return $this->hasMorph($relation, ['*'], $operator, $count, $boolean, $callback); } // If we only need to check for the existence of the relation, then we can optimize // the subquery to only run a "where exists" clause instead of this full "count" // clause. This will make these queries run much faster compared with a count. $method = $this->canUseExistsForExistenceCheck($operator, $count) ? 'getRelationExistenceQuery' : 'getRelationExistenceCountQuery'; $hasQuery = $relation->{$method}( $relation->getRelated()->newQueryWithoutRelationships(), $this ); // Next we will call any given callback as an "anonymous" scope so they can get the // proper logical grouping of the where clauses if needed by this Eloquent query // builder. Then, we will be ready to finalize and return this query instance. if ($callback) { $hasQuery->callScope($callback); } return $this->addHasWhere( $hasQuery, $relation, $operator, $count, $boolean ); }
Add a relationship count / exists condition to the query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param string $operator @param int $count @param string $boolean @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @return $this @throws \RuntimeException
has
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
protected function hasNested($relations, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) { $relations = explode('.', $relations); $doesntHave = $operator === '<' && $count === 1; if ($doesntHave) { $operator = '>='; $count = 1; } $closure = function ($q) use (&$closure, &$relations, $operator, $count, $callback) { // In order to nest "has", we need to add count relation constraints on the // callback Closure. We'll do this by simply passing the Closure its own // reference to itself so it calls itself recursively on each segment. count($relations) > 1 ? $q->whereHas(array_shift($relations), $closure) : $q->has(array_shift($relations), $operator, $count, 'and', $callback); }; return $this->has(array_shift($relations), $doesntHave ? '<' : '>=', 1, $boolean, $closure); }
Add nested relationship count / exists conditions to the query. Sets up recursive call to whereHas until we finish the nested relation. @param string $relations @param string $operator @param int $count @param string $boolean @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>): mixed)|null $callback @return $this
hasNested
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orHas($relation, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'or'); }
Add a relationship count / exists condition to the query with an "or". @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation @param string $operator @param int $count @return $this
orHas
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function doesntHave($relation, $boolean = 'and', ?Closure $callback = null) { return $this->has($relation, '<', 1, $boolean, $callback); }
Add a relationship count / exists condition to the query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param string $boolean @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @return $this
doesntHave
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orDoesntHave($relation) { return $this->doesntHave($relation, 'or'); }
Add a relationship count / exists condition to the query with an "or". @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation @return $this
orDoesntHave
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'and', $callback); }
Add a relationship count / exists condition to the query with where clauses. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @param string $operator @param int $count @return $this
whereHas
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function withWhereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1) { return $this->whereHas(Str::before($relation, ':'), $callback, $operator, $count) ->with($callback ? [$relation => fn ($query) => $callback($query)] : $relation); }
Add a relationship count / exists condition to the query with where clauses. Also load the relationship with the same condition. @param string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|null $callback @param string $operator @param int $count @return $this
withWhereHas
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'or', $callback); }
Add a relationship count / exists condition to the query with where clauses and an "or". @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @param string $operator @param int $count @return $this
orWhereHas
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereDoesntHave($relation, ?Closure $callback = null) { return $this->doesntHave($relation, 'and', $callback); }
Add a relationship count / exists condition to the query with where clauses. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @return $this
whereDoesntHave
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereDoesntHave($relation, ?Closure $callback = null) { return $this->doesntHave($relation, 'or', $callback); }
Add a relationship count / exists condition to the query with where clauses and an "or". @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback @return $this
orWhereDoesntHave
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null) { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } $types = (array) $types; $checkMorphNull = $types === ['*'] && (($operator === '<' && $count >= 1) || ($operator === '<=' && $count >= 0) || ($operator === '=' && $count === 0) || ($operator === '!=' && $count >= 1)); if ($types === ['*']) { $types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType()) ->filter() ->map(fn ($item) => enum_value($item)) ->all(); } if (empty($types)) { return $this->where(new Expression('0'), $operator, $count, $boolean); } foreach ($types as &$type) { $type = Relation::getMorphedModel($type) ?? $type; } return $this->where(function ($query) use ($relation, $callback, $operator, $count, $types) { foreach ($types as $type) { $query->orWhere(function ($query) use ($relation, $callback, $operator, $count, $type) { $belongsTo = $this->getBelongsToRelation($relation, $type); if ($callback) { $callback = function ($query) use ($callback, $type) { return $callback($query, $type); }; } $query->where($this->qualifyColumn($relation->getMorphType()), '=', (new $type)->getMorphClass()) ->whereHas($belongsTo, $callback, $operator, $count); }); } }, null, null, $boolean) ->when($checkMorphNull, fn (self $query) => $query->orWhereMorphedTo($relation, null)); }
Add a polymorphic relationship count / exists condition to the query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param string $operator @param int $count @param string $boolean @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @return $this
hasMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
protected function getBelongsToRelation(MorphTo $relation, $type) { $belongsTo = Relation::noConstraints(function () use ($relation, $type) { return $this->model->belongsTo( $type, $relation->getForeignKeyName(), $relation->getOwnerKeyName() ); }); $belongsTo->getQuery()->mergeConstraintsFrom($relation->getQuery()); return $belongsTo; }
Get the BelongsTo relationship for a single polymorphic type. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, TDeclaringModel> $relation @param class-string<TRelatedModel> $type @return \Illuminate\Database\Eloquent\Relations\BelongsTo<TRelatedModel, TDeclaringModel>
getBelongsToRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orHasMorph($relation, $types, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'or'); }
Add a polymorphic relationship count / exists condition to the query with an "or". @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param string|array<int, string> $types @param string $operator @param int $count @return $this
orHasMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function doesntHaveMorph($relation, $types, $boolean = 'and', ?Closure $callback = null) { return $this->hasMorph($relation, $types, '<', 1, $boolean, $callback); }
Add a polymorphic relationship count / exists condition to the query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param string $boolean @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @return $this
doesntHaveMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orDoesntHaveMorph($relation, $types) { return $this->doesntHaveMorph($relation, $types, 'or'); }
Add a polymorphic relationship count / exists condition to the query with an "or". @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param string|array<int, string> $types @return $this
orDoesntHaveMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereHasMorph($relation, $types, ?Closure $callback = null, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'and', $callback); }
Add a polymorphic relationship count / exists condition to the query with where clauses. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @param string $operator @param int $count @return $this
whereHasMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereHasMorph($relation, $types, ?Closure $callback = null, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'or', $callback); }
Add a polymorphic relationship count / exists condition to the query with where clauses and an "or". @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @param string $operator @param int $count @return $this
orWhereHasMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereDoesntHaveMorph($relation, $types, ?Closure $callback = null) { return $this->doesntHaveMorph($relation, $types, 'and', $callback); }
Add a polymorphic relationship count / exists condition to the query with where clauses. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @return $this
whereDoesntHaveMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereDoesntHaveMorph($relation, $types, ?Closure $callback = null) { return $this->doesntHaveMorph($relation, $types, 'or', $callback); }
Add a polymorphic relationship count / exists condition to the query with where clauses and an "or". @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback @return $this
orWhereDoesntHaveMorph
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereRelation($relation, $column, $operator = null, $value = null) { return $this->whereHas($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); }
Add a basic where clause to a relationship query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
whereRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function withWhereRelation($relation, $column, $operator = null, $value = null) { return $this->whereRelation($relation, $column, $operator, $value) ->with([ $relation => fn ($query) => $column instanceof Closure ? $column($query) : $query->where($column, $operator, $value), ]); }
Add a basic where clause to a relationship query and eager-load the relationship with the same conditions. @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
withWhereRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereRelation($relation, $column, $operator = null, $value = null) { return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); }
Add an "or where" clause to a relationship query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
orWhereRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereDoesntHaveRelation($relation, $column, $operator = null, $value = null) { return $this->whereDoesntHave($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); }
Add a basic count / exists condition to a relationship query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
whereDoesntHaveRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereDoesntHaveRelation($relation, $column, $operator = null, $value = null) { return $this->orWhereDoesntHave($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); }
Add an "or where" clause to a relationship query. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
orWhereDoesntHaveRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereMorphRelation($relation, $types, $column, $operator = null, $value = null) { return $this->whereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); }
Add a polymorphic relationship condition to the query with a where clause. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
whereMorphRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereMorphRelation($relation, $types, $column, $operator = null, $value = null) { return $this->orWhereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); }
Add a polymorphic relationship condition to the query with an "or where" clause. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
orWhereMorphRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereMorphDoesntHaveRelation($relation, $types, $column, $operator = null, $value = null) { return $this->whereDoesntHaveMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); }
Add a polymorphic relationship condition to the query with a doesn't have clause. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
whereMorphDoesntHaveRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereMorphDoesntHaveRelation($relation, $types, $column, $operator = null, $value = null) { return $this->orWhereDoesntHaveMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); }
Add a polymorphic relationship condition to the query with an "or doesn't have" clause. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation @param string|array<int, string> $types @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column @param mixed $operator @param mixed $value @return $this
orWhereMorphDoesntHaveRelation
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereMorphedTo($relation, $model, $boolean = 'and') { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } if (is_null($model)) { return $this->whereNull($relation->qualifyColumn($relation->getMorphType()), $boolean); } if (is_string($model)) { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array($model, $morphMap)) { $model = array_search($model, $morphMap, true); } return $this->where($relation->qualifyColumn($relation->getMorphType()), $model, null, $boolean); } $models = BaseCollection::wrap($model); if ($models->isEmpty()) { throw new InvalidArgumentException('Collection given to whereMorphedTo method may not be empty.'); } return $this->where(function ($query) use ($relation, $models) { $models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) { $query->orWhere(function ($query) use ($relation, $models) { $query->where($relation->qualifyColumn($relation->getMorphType()), $models->first()->getMorphClass()) ->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey()); }); }); }, null, null, $boolean); }
Add a morph-to relationship condition to the query. @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string|null $model @return $this
whereMorphedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereNotMorphedTo($relation, $model, $boolean = 'and') { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } if (is_string($model)) { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array($model, $morphMap)) { $model = array_search($model, $morphMap, true); } return $this->whereNot($relation->qualifyColumn($relation->getMorphType()), '<=>', $model, $boolean); } $models = BaseCollection::wrap($model); if ($models->isEmpty()) { throw new InvalidArgumentException('Collection given to whereNotMorphedTo method may not be empty.'); } return $this->whereNot(function ($query) use ($relation, $models) { $models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) { $query->orWhere(function ($query) use ($relation, $models) { $query->where($relation->qualifyColumn($relation->getMorphType()), '<=>', $models->first()->getMorphClass()) ->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey()); }); }); }, null, null, $boolean); }
Add a not morph-to relationship condition to the query. @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string $model @return $this
whereNotMorphedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereMorphedTo($relation, $model) { return $this->whereMorphedTo($relation, $model, 'or'); }
Add a morph-to relationship condition to the query with an "or where" clause. @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string|null $model @return $this
orWhereMorphedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereNotMorphedTo($relation, $model) { return $this->whereNotMorphedTo($relation, $model, 'or'); }
Add a not morph-to relationship condition to the query with an "or where" clause. @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string $model @return $this
orWhereNotMorphedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and') { if (! $related instanceof EloquentCollection) { $relatedCollection = $related->newCollection([$related]); } else { $relatedCollection = $related; $related = $relatedCollection->first(); } if ($relatedCollection->isEmpty()) { throw new InvalidArgumentException('Collection given to whereBelongsTo method may not be empty.'); } if ($relationshipName === null) { $relationshipName = Str::camel(class_basename($related)); } try { $relationship = $this->model->{$relationshipName}(); } catch (BadMethodCallException) { throw RelationNotFoundException::make($this->model, $relationshipName); } if (! $relationship instanceof BelongsTo) { throw RelationNotFoundException::make($this->model, $relationshipName, BelongsTo::class); } $this->whereIn( $relationship->getQualifiedForeignKeyName(), $relatedCollection->pluck($relationship->getOwnerKeyName())->toArray(), $boolean, ); return $this; }
Add a "belongs to" relationship where clause to the query. @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model> $related @param string|null $relationshipName @param string $boolean @return $this @throws \Illuminate\Database\Eloquent\RelationNotFoundException
whereBelongsTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereBelongsTo($related, $relationshipName = null) { return $this->whereBelongsTo($related, $relationshipName, 'or'); }
Add a "BelongsTo" relationship with an "or where" clause to the query. @param \Illuminate\Database\Eloquent\Model $related @param string|null $relationshipName @return $this @throws \RuntimeException
orWhereBelongsTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function whereAttachedTo($related, $relationshipName = null, $boolean = 'and') { $relatedCollection = $related instanceof EloquentCollection ? $related : $related->newCollection([$related]); $related = $relatedCollection->first(); if ($relatedCollection->isEmpty()) { throw new InvalidArgumentException('Collection given to whereAttachedTo method may not be empty.'); } if ($relationshipName === null) { $relationshipName = Str::plural(Str::camel(class_basename($related))); } try { $relationship = $this->model->{$relationshipName}(); } catch (BadMethodCallException) { throw RelationNotFoundException::make($this->model, $relationshipName); } if (! $relationship instanceof BelongsToMany) { throw RelationNotFoundException::make($this->model, $relationshipName, BelongsToMany::class); } $this->has( $relationshipName, boolean: $boolean, callback: fn (Builder $query) => $query->whereKey($relatedCollection->pluck($related->getKeyName())), ); return $this; }
Add a "belongs to many" relationship where clause to the query. @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model> $related @param string|null $relationshipName @param string $boolean @return $this @throws \Illuminate\Database\Eloquent\RelationNotFoundException
whereAttachedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function orWhereAttachedTo($related, $relationshipName = null) { return $this->whereAttachedTo($related, $relationshipName, 'or'); }
Add a "belongs to many" relationship with an "or where" clause to the query. @param \Illuminate\Database\Eloquent\Model $related @param string|null $relationshipName @return $this @throws \RuntimeException
orWhereAttachedTo
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function withAggregate($relations, $column, $function = null) { if (empty($relations)) { return $this; } if (is_null($this->query->columns)) { $this->query->select([$this->query->from.'.*']); } $relations = is_array($relations) ? $relations : [$relations]; foreach ($this->parseWithRelations($relations) as $name => $constraints) { // First we will determine if the name has been aliased using an "as" clause on the name // and if it has we will extract the actual relationship name and the desired name of // the resulting column. This allows multiple aggregates on the same relationships. $segments = explode(' ', $name); unset($alias); if (count($segments) === 3 && Str::lower($segments[1]) === 'as') { [$name, $alias] = [$segments[0], $segments[2]]; } $relation = $this->getRelationWithoutConstraints($name); if ($function) { if ($this->getQuery()->getGrammar()->isExpression($column)) { $aggregateColumn = $this->getQuery()->getGrammar()->getValue($column); } else { $hashedColumn = $this->getRelationHashedColumn($column, $relation); $aggregateColumn = $this->getQuery()->getGrammar()->wrap( $column === '*' ? $column : $relation->getRelated()->qualifyColumn($hashedColumn) ); } $expression = $function === 'exists' ? $aggregateColumn : sprintf('%s(%s)', $function, $aggregateColumn); } else { $expression = $this->getQuery()->getGrammar()->getValue($column); } // Here, we will grab the relationship sub-query and prepare to add it to the main query // as a sub-select. First, we'll get the "has" query and use that to get the relation // sub-query. We'll format this relationship name and append this column if needed. $query = $relation->getRelationExistenceQuery( $relation->getRelated()->newQuery(), $this, new Expression($expression) )->setBindings([], 'select'); $query->callScope($constraints); $query = $query->mergeConstraintsFrom($relation->getQuery())->toBase(); // If the query contains certain elements like orderings / more than one column selected // then we will remove those elements from the query so that it will execute properly // when given to the database. Otherwise, we may receive SQL errors or poor syntax. $query->orders = null; $query->setBindings([], 'order'); if (count($query->columns) > 1) { $query->columns = [$query->columns[0]]; $query->bindings['select'] = []; } // Finally, we will make the proper column alias to the query and run this sub-select on // the query builder. Then, we will return the builder instance back to the developer // for further constraint chaining that needs to take place on the query as needed. $alias ??= Str::snake( preg_replace( '/[^[:alnum:][:space:]_]/u', '', sprintf('%s %s %s', $name, $function, strtolower($this->getQuery()->getGrammar()->getValue($column))) ) ); if ($function === 'exists') { $this->selectRaw( sprintf('exists(%s) as %s', $query->toSql(), $this->getQuery()->grammar->wrap($alias)), $query->getBindings() )->withCasts([$alias => 'bool']); } else { $this->selectSub( $function ? $query : $query->limit(1), $alias ); } } return $this; }
Add subselect queries to include an aggregate value for a relationship. @param mixed $relations @param \Illuminate\Contracts\Database\Query\Expression|string $column @param string $function @return $this
withAggregate
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
protected function getRelationHashedColumn($column, $relation) { if (str_contains($column, '.')) { return $column; } return $this->getQuery()->from === $relation->getQuery()->getQuery()->from ? "{$relation->getRelationCountHash(false)}.$column" : $column; }
Get the relation hashed column name for the given column and relation. @param string $column @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $relation @return string
getRelationHashedColumn
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function withCount($relations) { return $this->withAggregate(is_array($relations) ? $relations : func_get_args(), '*', 'count'); }
Add subselect queries to count the relations. @param mixed $relations @return $this
withCount
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT
public function withMax($relation, $column) { return $this->withAggregate($relation, $column, 'max'); }
Add subselect queries to include the max of the relation's column. @param string|array $relation @param \Illuminate\Contracts\Database\Query\Expression|string $column @return $this
withMax
php
illuminate/database
Eloquent/Concerns/QueriesRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
MIT