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
protected function getOriginalWithoutRewindingModel($key = null, $default = null) { if ($key) { return $this->transformModelValue( $key, Arr::get($this->original, $key, $default) ); } return (new Collection($this->original)) ->mapWithKeys(fn ($value, $key) => [$key => $this->transformModelValue($key, $value)]) ->all(); }
Get the model's original attribute values. @param string|null $key @param mixed $default @return ($key is null ? array<string, mixed> : mixed)
getOriginalWithoutRewindingModel
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getRawOriginal($key = null, $default = null) { return Arr::get($this->original, $key, $default); }
Get the model's raw original attribute values. @param string|null $key @param mixed $default @return ($key is null ? array<string, mixed> : mixed)
getRawOriginal
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function only($attributes) { $results = []; foreach (is_array($attributes) ? $attributes : func_get_args() as $attribute) { $results[$attribute] = $this->getAttribute($attribute); } return $results; }
Get a subset of the model's attributes. @param array<string>|mixed $attributes @return array<string, mixed>
only
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function except($attributes) { $attributes = is_array($attributes) ? $attributes : func_get_args(); $results = []; foreach ($this->getAttributes() as $key => $value) { if (! in_array($key, $attributes)) { $results[$key] = $this->getAttribute($key); } } return $results; }
Get all attributes except the given ones. @param array<string>|mixed $attributes @return array
except
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function syncOriginal() { $this->original = $this->getAttributes(); return $this; }
Sync the original attributes with the current. @return $this
syncOriginal
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function syncOriginalAttribute($attribute) { return $this->syncOriginalAttributes($attribute); }
Sync a single original attribute with its current value. @param string $attribute @return $this
syncOriginalAttribute
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function syncOriginalAttributes($attributes) { $attributes = is_array($attributes) ? $attributes : func_get_args(); $modelAttributes = $this->getAttributes(); foreach ($attributes as $attribute) { $this->original[$attribute] = $modelAttributes[$attribute]; } return $this; }
Sync multiple original attribute with their current values. @param array<string>|string $attributes @return $this
syncOriginalAttributes
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function syncChanges() { $this->changes = $this->getDirty(); $this->previous = array_intersect_key($this->getRawOriginal(), $this->changes); return $this; }
Sync the changed attributes. @return $this
syncChanges
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function isDirty($attributes = null) { return $this->hasChanges( $this->getDirty(), is_array($attributes) ? $attributes : func_get_args() ); }
Determine if the model or any of the given attribute(s) have been modified. @param array<string>|string|null $attributes @return bool
isDirty
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function isClean($attributes = null) { return ! $this->isDirty(...func_get_args()); }
Determine if the model or all the given attribute(s) have remained the same. @param array<string>|string|null $attributes @return bool
isClean
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function discardChanges() { [$this->attributes, $this->changes, $this->previous] = [$this->original, [], []]; $this->classCastCache = []; $this->attributeCastCache = []; return $this; }
Discard attribute changes and reset the attributes to their original state. @return $this
discardChanges
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function wasChanged($attributes = null) { return $this->hasChanges( $this->getChanges(), is_array($attributes) ? $attributes : func_get_args() ); }
Determine if the model or any of the given attribute(s) were changed when the model was last saved. @param array<string>|string|null $attributes @return bool
wasChanged
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
protected function hasChanges($changes, $attributes = null) { // If no specific attributes were provided, we will just see if the dirty array // already contains any attributes. If it does we will just return that this // count is greater than zero. Else, we need to check specific attributes. if (empty($attributes)) { return count($changes) > 0; } // Here we will spin through every attribute and see if this is in the array of // dirty attributes. If it is, we will return true and if we make it through // all of the attributes for the entire array we will return false at end. foreach (Arr::wrap($attributes) as $attribute) { if (array_key_exists($attribute, $changes)) { return true; } } return false; }
Determine if any of the given attributes were changed when the model was last saved. @param array<string> $changes @param array<string>|string|null $attributes @return bool
hasChanges
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getDirty() { $dirty = []; foreach ($this->getAttributes() as $key => $value) { if (! $this->originalIsEquivalent($key)) { $dirty[$key] = $value; } } return $dirty; }
Get the attributes that have been changed since the last sync. @return array<string, mixed>
getDirty
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
protected function getDirtyForUpdate() { return $this->getDirty(); }
Get the attributes that have been changed since the last sync for an update operation. @return array<string, mixed>
getDirtyForUpdate
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getChanges() { return $this->changes; }
Get the attributes that were changed when the model was last saved. @return array<string, mixed>
getChanges
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getPrevious() { return $this->previous; }
Get the attributes that were previously original before the model was last saved. @return array<string, mixed>
getPrevious
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function originalIsEquivalent($key) { if (! array_key_exists($key, $this->original)) { return false; } $attribute = Arr::get($this->attributes, $key); $original = Arr::get($this->original, $key); if ($attribute === $original) { return true; } elseif (is_null($attribute)) { return false; } elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) { return $this->fromDateTime($attribute) === $this->fromDateTime($original); } elseif ($this->hasCast($key, ['object', 'collection'])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->hasCast($key, ['real', 'float', 'double'])) { if ($original === null) { return false; } return abs($this->castAttribute($key, $attribute) - $this->castAttribute($key, $original)) < PHP_FLOAT_EPSILON * 4; } elseif ($this->isEncryptedCastable($key) && ! empty(static::currentEncrypter()->getPreviousKeys())) { return false; } elseif ($this->hasCast($key, static::$primitiveCastTypes)) { return $this->castAttribute($key, $attribute) === $this->castAttribute($key, $original); } elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->isClassCastable($key) && $original !== null && Str::startsWith($this->getCasts()[$key], [AsEncryptedArrayObject::class, AsEncryptedCollection::class])) { if (empty(static::currentEncrypter()->getPreviousKeys())) { return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original); } return false; } elseif ($this->isClassComparable($key)) { return $this->compareClassCastableAttribute($key, $original, $attribute); } return is_numeric($attribute) && is_numeric($original) && strcmp((string) $attribute, (string) $original) === 0; }
Determine if the new and old values for a given key are equivalent. @param string $key @return bool
originalIsEquivalent
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
protected function transformModelValue($key, $value) { // If the attribute has a get mutator, we will call that then return what // it returns as the value, which is useful for transforming values on // retrieval from the model to a form that is more useful for usage. if ($this->hasGetMutator($key)) { return $this->mutateAttribute($key, $value); } elseif ($this->hasAttributeGetMutator($key)) { return $this->mutateAttributeMarkedAttribute($key, $value); } // If the attribute exists within the cast array, we will convert it to // an appropriate native PHP type dependent upon the associated value // given with the key in the pair. Dayle made this comment line up. if ($this->hasCast($key)) { if (static::preventsAccessingMissingAttributes() && ! array_key_exists($key, $this->attributes) && ($this->isEnumCastable($key) || in_array($this->getCastType($key), static::$primitiveCastTypes))) { $this->throwMissingAttributeExceptionIfApplicable($key); } return $this->castAttribute($key, $value); } // If the attribute is listed as a date, we will convert it to a DateTime // instance on retrieval, which makes it quite convenient to work with // date fields without having to create a mutator for each property. if ($value !== null && \in_array($key, $this->getDates(), false)) { return $this->asDateTime($value); } return $value; }
Transform a raw model value using mutators, casts, etc. @param string $key @param mixed $value @return mixed
transformModelValue
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function append($attributes) { $this->appends = array_values(array_unique( array_merge($this->appends, is_string($attributes) ? func_get_args() : $attributes) )); return $this; }
Append attributes to query when building a query. @param array<string>|string $attributes @return $this
append
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getAppends() { return $this->appends; }
Get the accessors that are being appended to model arrays. @return array
getAppends
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function setAppends(array $appends) { $this->appends = $appends; return $this; }
Set the accessors to append to model arrays. @param array $appends @return $this
setAppends
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function hasAppended($attribute) { return in_array($attribute, $this->appends); }
Return whether the accessor attribute has been appended. @param string $attribute @return bool
hasAppended
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public function getMutatedAttributes() { if (! isset(static::$mutatorCache[static::class])) { static::cacheMutatedAttributes($this); } return static::$mutatorCache[static::class]; }
Get the mutated attributes for a given instance. @return array
getMutatedAttributes
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public static function cacheMutatedAttributes($classOrInstance) { $reflection = new ReflectionClass($classOrInstance); $class = $reflection->getName(); static::$getAttributeMutatorCache[$class] = (new Collection($attributeMutatorMethods = static::getAttributeMarkedMutatorMethods($classOrInstance))) ->mapWithKeys(fn ($match) => [lcfirst(static::$snakeAttributes ? Str::snake($match) : $match) => true]) ->all(); static::$mutatorCache[$class] = (new Collection(static::getMutatorMethods($class))) ->merge($attributeMutatorMethods) ->map(fn ($match) => lcfirst(static::$snakeAttributes ? Str::snake($match) : $match)) ->all(); }
Extract and cache all the mutated attributes of a class. @param object|string $classOrInstance @return void
cacheMutatedAttributes
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
protected static function getMutatorMethods($class) { preg_match_all('/(?<=^|;)get([^;]+?)Attribute(;|$)/', implode(';', get_class_methods($class)), $matches); return $matches[1]; }
Get all of the attribute mutator methods. @param mixed $class @return array
getMutatorMethods
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
protected static function getAttributeMarkedMutatorMethods($class) { $instance = is_object($class) ? $class : new $class; return (new Collection((new ReflectionClass($instance))->getMethods()))->filter(function ($method) use ($instance) { $returnType = $method->getReturnType(); if ($returnType instanceof ReflectionNamedType && $returnType->getName() === Attribute::class) { if (is_callable($method->invoke($instance)->get)) { return true; } } return false; })->map->name->values()->all(); }
Get all of the "Attribute" return typed attribute mutator methods. @param mixed $class @return array
getAttributeMarkedMutatorMethods
php
illuminate/database
Eloquent/Concerns/HasAttributes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
MIT
public static function bootHasEvents() { static::whenBooted(fn () => static::observe(static::resolveObserveAttributes())); }
Boot the has event trait for a model. @return void
bootHasEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function resolveObserveAttributes() { $reflectionClass = new ReflectionClass(static::class); $isEloquentGrandchild = is_subclass_of(static::class, Model::class) && get_parent_class(static::class) !== Model::class; return (new Collection($reflectionClass->getAttributes(ObservedBy::class))) ->map(fn ($attribute) => $attribute->getArguments()) ->flatten() ->when($isEloquentGrandchild, function (Collection $attributes) { return (new Collection(get_parent_class(static::class)::resolveObserveAttributes())) ->merge($attributes); }) ->all(); }
Resolve the observe class names from the attributes. @return array
resolveObserveAttributes
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function observe($classes) { $instance = new static; foreach (Arr::wrap($classes) as $class) { $instance->registerObserver($class); } }
Register observers with the model. @param object|array|string $classes @return void @throws \RuntimeException
observe
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
protected function registerObserver($class) { $className = $this->resolveObserverClassName($class); // When registering a model observer, we will spin through the possible events // and determine if this observer has that method. If it does, we will hook // it into the model's event system, making it convenient to watch these. foreach ($this->getObservableEvents() as $event) { if (method_exists($class, $event)) { static::registerModelEvent($event, $className.'@'.$event); } } }
Register a single observer with the model. @param object|string $class @return void @throws \RuntimeException
registerObserver
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
private function resolveObserverClassName($class) { if (is_object($class)) { return get_class($class); } if (class_exists($class)) { return $class; } throw new InvalidArgumentException('Unable to find observer: '.$class); }
Resolve the observer's class name from an object or string. @param object|string $class @return string @throws \InvalidArgumentException
resolveObserverClassName
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public function getObservableEvents() { return array_merge( [ 'retrieved', 'creating', 'created', 'updating', 'updated', 'saving', 'saved', 'restoring', 'restored', 'replicating', 'trashed', 'deleting', 'deleted', 'forceDeleting', 'forceDeleted', ], $this->observables ); }
Get the observable event names. @return array
getObservableEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public function setObservableEvents(array $observables) { $this->observables = $observables; return $this; }
Set the observable event names. @param array $observables @return $this
setObservableEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public function addObservableEvents($observables) { $this->observables = array_unique(array_merge( $this->observables, is_array($observables) ? $observables : func_get_args() )); }
Add an observable event name. @param array|mixed $observables @return void
addObservableEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public function removeObservableEvents($observables) { $this->observables = array_diff( $this->observables, is_array($observables) ? $observables : func_get_args() ); }
Remove an observable event name. @param array|mixed $observables @return void
removeObservableEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
protected static function registerModelEvent($event, $callback) { if (isset(static::$dispatcher)) { $name = static::class; static::$dispatcher->listen("eloquent.{$event}: {$name}", $callback); } }
Register a model event with the dispatcher. @param string $event @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
registerModelEvent
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
protected function fireModelEvent($event, $halt = true) { if (! isset(static::$dispatcher)) { return true; } // First, we will get the proper method to call on the event dispatcher, and then we // will attempt to fire a custom, object based event for the given event. If that // returns a result we can return that result, or we'll call the string events. $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( $this->fireCustomModelEvent($event, $method) ); if ($result === false) { return false; } return ! empty($result) ? $result : static::$dispatcher->{$method}( "eloquent.{$event}: ".static::class, $this ); }
Fire the given event for the model. @param string $event @param bool $halt @return mixed
fireModelEvent
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
protected function fireCustomModelEvent($event, $method) { if (! isset($this->dispatchesEvents[$event])) { return; } $result = static::$dispatcher->$method(new $this->dispatchesEvents[$event]($this)); if (! is_null($result)) { return $result; } }
Fire a custom model event for the given event. @param string $event @param string $method @return mixed|null
fireCustomModelEvent
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
protected function filterModelEventResults($result) { if (is_array($result)) { $result = array_filter($result, function ($response) { return ! is_null($response); }); } return $result; }
Filter the model event results. @param mixed $result @return mixed
filterModelEventResults
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function retrieved($callback) { static::registerModelEvent('retrieved', $callback); }
Register a retrieved model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
retrieved
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function saving($callback) { static::registerModelEvent('saving', $callback); }
Register a saving model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
saving
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function saved($callback) { static::registerModelEvent('saved', $callback); }
Register a saved model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
saved
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function updating($callback) { static::registerModelEvent('updating', $callback); }
Register an updating model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
updating
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function updated($callback) { static::registerModelEvent('updated', $callback); }
Register an updated model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
updated
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function creating($callback) { static::registerModelEvent('creating', $callback); }
Register a creating model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
creating
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function created($callback) { static::registerModelEvent('created', $callback); }
Register a created model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
created
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function replicating($callback) { static::registerModelEvent('replicating', $callback); }
Register a replicating model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
replicating
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function deleting($callback) { static::registerModelEvent('deleting', $callback); }
Register a deleting model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
deleting
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function deleted($callback) { static::registerModelEvent('deleted', $callback); }
Register a deleted model event with the dispatcher. @param \Illuminate\Events\QueuedClosure|callable|array|class-string $callback @return void
deleted
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function flushEventListeners() { if (! isset(static::$dispatcher)) { return; } $instance = new static; foreach ($instance->getObservableEvents() as $event) { static::$dispatcher->forget("eloquent.{$event}: ".static::class); } foreach ($instance->dispatchesEvents as $event) { static::$dispatcher->forget($event); } }
Remove all the event listeners for the model. @return void
flushEventListeners
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public function dispatchesEvents() { return $this->dispatchesEvents; }
Get the event map for the model. @return array
dispatchesEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function getEventDispatcher() { return static::$dispatcher; }
Get the event dispatcher instance. @return \Illuminate\Contracts\Events\Dispatcher|null
getEventDispatcher
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function setEventDispatcher(Dispatcher $dispatcher) { static::$dispatcher = $dispatcher; }
Set the event dispatcher instance. @param \Illuminate\Contracts\Events\Dispatcher $dispatcher @return void
setEventDispatcher
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function unsetEventDispatcher() { static::$dispatcher = null; }
Unset the event dispatcher for models. @return void
unsetEventDispatcher
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function withoutEvents(callable $callback) { $dispatcher = static::getEventDispatcher(); if ($dispatcher) { static::setEventDispatcher(new NullDispatcher($dispatcher)); } try { return $callback(); } finally { if ($dispatcher) { static::setEventDispatcher($dispatcher); } } }
Execute a callback without firing any model events for any model type. @param callable $callback @return mixed
withoutEvents
php
illuminate/database
Eloquent/Concerns/HasEvents.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasEvents.php
MIT
public static function bootHasGlobalScopes() { static::addGlobalScopes(static::resolveGlobalScopeAttributes()); }
Boot the has global scopes trait for a model. @return void
bootHasGlobalScopes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function resolveGlobalScopeAttributes() { $reflectionClass = new ReflectionClass(static::class); return (new Collection($reflectionClass->getAttributes(ScopedBy::class))) ->map(fn ($attribute) => $attribute->getArguments()) ->flatten() ->all(); }
Resolve the global scope class names from the attributes. @return array
resolveGlobalScopeAttributes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function addGlobalScope($scope, $implementation = null) { if (is_string($scope) && ($implementation instanceof Closure || $implementation instanceof Scope)) { return static::$globalScopes[static::class][$scope] = $implementation; } elseif ($scope instanceof Closure) { return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope; } elseif ($scope instanceof Scope) { return static::$globalScopes[static::class][get_class($scope)] = $scope; } elseif (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class)) { return static::$globalScopes[static::class][$scope] = new $scope; } throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope or be a class name of a class extending '.Scope::class); }
Register a new global scope on the model. @param \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|string $scope @param \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|null $implementation @return mixed @throws \InvalidArgumentException
addGlobalScope
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function addGlobalScopes(array $scopes) { foreach ($scopes as $key => $scope) { if (is_string($key)) { static::addGlobalScope($key, $scope); } else { static::addGlobalScope($scope); } } }
Register multiple global scopes on the model. @param array $scopes @return void
addGlobalScopes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function hasGlobalScope($scope) { return ! is_null(static::getGlobalScope($scope)); }
Determine if a model has a global scope. @param \Illuminate\Database\Eloquent\Scope|string $scope @return bool
hasGlobalScope
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function getGlobalScope($scope) { if (is_string($scope)) { return Arr::get(static::$globalScopes, static::class.'.'.$scope); } return Arr::get( static::$globalScopes, static::class.'.'.get_class($scope) ); }
Get a global scope registered with the model. @param \Illuminate\Database\Eloquent\Scope|string $scope @return \Illuminate\Database\Eloquent\Scope|(\Closure(\Illuminate\Database\Eloquent\Builder<static>): mixed)|null
getGlobalScope
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function getAllGlobalScopes() { return static::$globalScopes; }
Get all of the global scopes that are currently registered. @return array
getAllGlobalScopes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public static function setAllGlobalScopes($scopes) { static::$globalScopes = $scopes; }
Set the current global scopes. @param array $scopes @return void
setAllGlobalScopes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public function getGlobalScopes() { return Arr::get(static::$globalScopes, static::class, []); }
Get the global scopes for this class instance. @return array
getGlobalScopes
php
illuminate/database
Eloquent/Concerns/HasGlobalScopes.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasGlobalScopes.php
MIT
public function relationResolver($class, $key) { if ($resolver = static::$relationResolvers[$class][$key] ?? null) { return $resolver; } if ($parent = get_parent_class($class)) { return $this->relationResolver($parent, $key); } return null; }
Get the dynamic relation resolver if defined or inherited, or return null. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $class @param string $key @return Closure|null
relationResolver
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public static function resolveRelationUsing($name, Closure $callback) { static::$relationResolvers = array_replace_recursive( static::$relationResolvers, [static::class => [$name => $callback]] ); }
Define a dynamic relation resolver. @param string $name @param \Closure $callback @return void
resolveRelationUsing
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function hasRelationAutoloadCallback() { return ! is_null($this->relationAutoloadCallback); }
Determine if a relationship autoloader callback has been defined. @return bool
hasRelationAutoloadCallback
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function autoloadRelationsUsing(Closure $callback, $context = null) { // Prevent circular relation autoloading... if ($context && $this->relationAutoloadContext === $context) { return $this; } $this->relationAutoloadCallback = $callback; $this->relationAutoloadContext = $context; foreach ($this->relations as $key => $value) { $this->propagateRelationAutoloadCallbackToRelation($key, $value); } return $this; }
Define an automatic relationship autoloader callback for this model and its relations. @param \Closure $callback @param mixed $context @return $this
autoloadRelationsUsing
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function attemptToAutoloadRelation($key) { if (! $this->hasRelationAutoloadCallback()) { return false; } $this->invokeRelationAutoloadCallbackFor($key, []); return $this->relationLoaded($key); }
Attempt to autoload the given relationship using the autoload callback. @param string $key @return bool
attemptToAutoloadRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function invokeRelationAutoloadCallbackFor($key, $tuples) { $tuples = array_merge([[$key, get_class($this)]], $tuples); call_user_func($this->relationAutoloadCallback, $tuples); }
Invoke the relationship autoloader callback for the given relationships. @param string $key @param array $tuples @return void
invokeRelationAutoloadCallbackFor
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function propagateRelationAutoloadCallbackToRelation($key, $models) { if (! $this->hasRelationAutoloadCallback() || ! $models) { return; } if ($models instanceof Model) { $models = [$models]; } if (! is_iterable($models)) { return; } $callback = fn (array $tuples) => $this->invokeRelationAutoloadCallbackFor($key, $tuples); foreach ($models as $model) { $model->autoloadRelationsUsing($callback, $this->relationAutoloadContext); } }
Propagate the relationship autoloader callback to the given related models. @param string $key @param mixed $models @return void
propagateRelationAutoloadCallbackToRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function hasOne($related, $foreignKey = null, $localKey = null) { $instance = $this->newRelatedInstance($related); $foreignKey = $foreignKey ?: $this->getForeignKey(); $localKey = $localKey ?: $this->getKeyName(); return $this->newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey); }
Define a one-to-one relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string|null $foreignKey @param string|null $localKey @return \Illuminate\Database\Eloquent\Relations\HasOne<TRelatedModel, $this>
hasOne
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) { return new HasOne($query, $parent, $foreignKey, $localKey); }
Instantiate a new HasOne relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $foreignKey @param string $localKey @return \Illuminate\Database\Eloquent\Relations\HasOne<TRelatedModel, TDeclaringModel>
newHasOne
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function hasOneThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { $through = $this->newRelatedThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); $secondKey = $secondKey ?: $through->getForeignKey(); return $this->newHasOneThrough( $this->newRelatedInstance($related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey ?: $this->getKeyName(), $secondLocalKey ?: $through->getKeyName(), ); }
Define a has-one-through relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TIntermediateModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param class-string<TIntermediateModel> $through @param string|null $firstKey @param string|null $secondKey @param string|null $localKey @param string|null $secondLocalKey @return \Illuminate\Database\Eloquent\Relations\HasOneThrough<TRelatedModel, TIntermediateModel, $this>
hasOneThrough
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); }
Instantiate a new HasOneThrough relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TIntermediateModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $farParent @param TIntermediateModel $throughParent @param string $firstKey @param string $secondKey @param string $localKey @param string $secondLocalKey @return \Illuminate\Database\Eloquent\Relations\HasOneThrough<TRelatedModel, TIntermediateModel, TDeclaringModel>
newHasOneThrough
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function morphOne($related, $name, $type = null, $id = null, $localKey = null) { $instance = $this->newRelatedInstance($related); [$type, $id] = $this->getMorphs($name, $type, $id); $localKey = $localKey ?: $this->getKeyName(); return $this->newMorphOne($instance->newQuery(), $this, $instance->qualifyColumn($type), $instance->qualifyColumn($id), $localKey); }
Define a polymorphic one-to-one relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string $name @param string|null $type @param string|null $id @param string|null $localKey @return \Illuminate\Database\Eloquent\Relations\MorphOne<TRelatedModel, $this>
morphOne
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) { return new MorphOne($query, $parent, $type, $id, $localKey); }
Instantiate a new MorphOne relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $type @param string $id @param string $localKey @return \Illuminate\Database\Eloquent\Relations\MorphOne<TRelatedModel, TDeclaringModel>
newMorphOne
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) { // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most // of the time this will be what we desire to use for the relationships. if (is_null($relation)) { $relation = $this->guessBelongsToRelation(); } $instance = $this->newRelatedInstance($related); // If no foreign key was supplied, we can use a backtrace to guess the proper // foreign key name by using the name of the relationship function, which // when combined with an "_id" should conventionally match the columns. if (is_null($foreignKey)) { $foreignKey = Str::snake($relation).'_'.$instance->getKeyName(); } // Once we have the foreign key names we'll just create a new Eloquent query // for the related models and return the relationship instance which will // actually be responsible for retrieving and hydrating every relation. $ownerKey = $ownerKey ?: $instance->getKeyName(); return $this->newBelongsTo( $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation ); }
Define an inverse one-to-one or many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string|null $foreignKey @param string|null $ownerKey @param string|null $relation @return \Illuminate\Database\Eloquent\Relations\BelongsTo<TRelatedModel, $this>
belongsTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) { return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); }
Instantiate a new BelongsTo relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $child @param string $foreignKey @param string $ownerKey @param string $relation @return \Illuminate\Database\Eloquent\Relations\BelongsTo<TRelatedModel, TDeclaringModel>
newBelongsTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function morphTo($name = null, $type = null, $id = null, $ownerKey = null) { // If no name is provided, we will use the backtrace to get the function name // since that is most likely the name of the polymorphic interface. We can // use that to get both the class and foreign key that will be utilized. $name = $name ?: $this->guessBelongsToRelation(); [$type, $id] = $this->getMorphs( Str::snake($name), $type, $id ); // If the type value is null it is probably safe to assume we're eager loading // the relationship. In this case we'll just pass in a dummy query where we // need to remove any eager loads that may already be defined on a model. return is_null($class = $this->getAttributeFromArray($type)) || $class === '' ? $this->morphEagerTo($name, $type, $id, $ownerKey) : $this->morphInstanceTo($class, $name, $type, $id, $ownerKey); }
Define a polymorphic, inverse one-to-one or many relationship. @param string|null $name @param string|null $type @param string|null $id @param string|null $ownerKey @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
morphTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function morphEagerTo($name, $type, $id, $ownerKey) { return $this->newMorphTo( $this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name ); }
Define a polymorphic, inverse one-to-one or many relationship. @param string $name @param string $type @param string $id @param string|null $ownerKey @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
morphEagerTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function morphInstanceTo($target, $name, $type, $id, $ownerKey) { $instance = $this->newRelatedInstance( static::getActualClassNameForMorph($target) ); return $this->newMorphTo( $instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name ); }
Define a polymorphic, inverse one-to-one or many relationship. @param string $target @param string $name @param string $type @param string $id @param string|null $ownerKey @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
morphInstanceTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) { return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); }
Instantiate a new MorphTo relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $foreignKey @param string|null $ownerKey @param string $type @param string $relation @return \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, TDeclaringModel>
newMorphTo
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public static function getActualClassNameForMorph($class) { return Arr::get(Relation::morphMap() ?: [], $class, $class); }
Retrieve the actual class name for a given morph class. @param string $class @return string
getActualClassNameForMorph
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function guessBelongsToRelation() { [$one, $two, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); return $caller['function']; }
Guess the "belongs to" relationship name. @return string
guessBelongsToRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function through($relationship) { if (is_string($relationship)) { $relationship = $this->{$relationship}(); } return new PendingHasThroughRelationship($this, $relationship); }
Create a pending has-many-through or has-one-through relationship. @template TIntermediateModel of \Illuminate\Database\Eloquent\Model @param string|\Illuminate\Database\Eloquent\Relations\HasMany<TIntermediateModel, covariant $this>|\Illuminate\Database\Eloquent\Relations\HasOne<TIntermediateModel, covariant $this> $relationship @return ( $relationship is string ? \Illuminate\Database\Eloquent\PendingHasThroughRelationship<\Illuminate\Database\Eloquent\Model, $this> : ( $relationship is \Illuminate\Database\Eloquent\Relations\HasMany<TIntermediateModel, $this> ? \Illuminate\Database\Eloquent\PendingHasThroughRelationship<TIntermediateModel, $this, \Illuminate\Database\Eloquent\Relations\HasMany<TIntermediateModel, $this>> : \Illuminate\Database\Eloquent\PendingHasThroughRelationship<TIntermediateModel, $this, \Illuminate\Database\Eloquent\Relations\HasOne<TIntermediateModel, $this>> ) )
through
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function hasMany($related, $foreignKey = null, $localKey = null) { $instance = $this->newRelatedInstance($related); $foreignKey = $foreignKey ?: $this->getForeignKey(); $localKey = $localKey ?: $this->getKeyName(); return $this->newHasMany( $instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey ); }
Define a one-to-many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string|null $foreignKey @param string|null $localKey @return \Illuminate\Database\Eloquent\Relations\HasMany<TRelatedModel, $this>
hasMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) { return new HasMany($query, $parent, $foreignKey, $localKey); }
Instantiate a new HasMany relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $foreignKey @param string $localKey @return \Illuminate\Database\Eloquent\Relations\HasMany<TRelatedModel, TDeclaringModel>
newHasMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { $through = $this->newRelatedThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); $secondKey = $secondKey ?: $through->getForeignKey(); return $this->newHasManyThrough( $this->newRelatedInstance($related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey ?: $this->getKeyName(), $secondLocalKey ?: $through->getKeyName() ); }
Define a has-many-through relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TIntermediateModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param class-string<TIntermediateModel> $through @param string|null $firstKey @param string|null $secondKey @param string|null $localKey @param string|null $secondLocalKey @return \Illuminate\Database\Eloquent\Relations\HasManyThrough<TRelatedModel, TIntermediateModel, $this>
hasManyThrough
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); }
Instantiate a new HasManyThrough relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TIntermediateModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $farParent @param TIntermediateModel $throughParent @param string $firstKey @param string $secondKey @param string $localKey @param string $secondLocalKey @return \Illuminate\Database\Eloquent\Relations\HasManyThrough<TRelatedModel, TIntermediateModel, TDeclaringModel>
newHasManyThrough
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function morphMany($related, $name, $type = null, $id = null, $localKey = null) { $instance = $this->newRelatedInstance($related); // Here we will gather up the morph type and ID for the relationship so that we // can properly query the intermediate table of a relation. Finally, we will // get the table and create the relationship instances for the developers. [$type, $id] = $this->getMorphs($name, $type, $id); $localKey = $localKey ?: $this->getKeyName(); return $this->newMorphMany($instance->newQuery(), $this, $instance->qualifyColumn($type), $instance->qualifyColumn($id), $localKey); }
Define a polymorphic one-to-many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string $name @param string|null $type @param string|null $id @param string|null $localKey @return \Illuminate\Database\Eloquent\Relations\MorphMany<TRelatedModel, $this>
morphMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) { return new MorphMany($query, $parent, $type, $id, $localKey); }
Instantiate a new MorphMany relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $type @param string $id @param string $localKey @return \Illuminate\Database\Eloquent\Relations\MorphMany<TRelatedModel, TDeclaringModel>
newMorphMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function belongsToMany( $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null, ) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. if (is_null($relation)) { $relation = $this->guessBelongsToManyRelation(); } // First, we'll need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we'll make the query // instances as well as the relationship instances we need for this. $instance = $this->newRelatedInstance($related); $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. if (is_null($table)) { $table = $this->joiningTable($related, $instance); } return $this->newBelongsToMany( $instance->newQuery(), $this, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation, ); }
Define a many-to-many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string|class-string<\Illuminate\Database\Eloquent\Model>|null $table @param string|null $foreignPivotKey @param string|null $relatedPivotKey @param string|null $parentKey @param string|null $relatedKey @param string|null $relation @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, $this, \Illuminate\Database\Eloquent\Relations\Pivot>
belongsToMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newBelongsToMany( Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, ) { return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); }
Instantiate a new BelongsToMany relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string|class-string<\Illuminate\Database\Eloquent\Model> $table @param string $foreignPivotKey @param string $relatedPivotKey @param string $parentKey @param string $relatedKey @param string|null $relationName @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Relations\Pivot>
newBelongsToMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function morphToMany( $related, $name, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null, $inverse = false, ) { $relation = $relation ?: $this->guessBelongsToManyRelation(); // First, we will need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we will make the query // instances, as well as the relationship instances we need for these. $instance = $this->newRelatedInstance($related); $foreignPivotKey = $foreignPivotKey ?: $name.'_id'; $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); // Now we're ready to create a new query builder for the related model and // the relationship instances for this relation. This relation will set // appropriate query constraints then entirely manage the hydrations. if (! $table) { $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE); $lastWord = array_pop($words); $table = implode('', $words).Str::plural($lastWord); } return $this->newMorphToMany( $instance->newQuery(), $this, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation, $inverse, ); }
Define a polymorphic many-to-many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string $name @param string|null $table @param string|null $foreignPivotKey @param string|null $relatedPivotKey @param string|null $parentKey @param string|null $relatedKey @param string|null $relation @param bool $inverse @return \Illuminate\Database\Eloquent\Relations\MorphToMany<TRelatedModel, $this>
morphToMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function newMorphToMany( Builder $query, Model $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false, ) { return new MorphToMany( $query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName, $inverse, ); }
Instantiate a new MorphToMany relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @template TDeclaringModel of \Illuminate\Database\Eloquent\Model @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query @param TDeclaringModel $parent @param string $name @param string $table @param string $foreignPivotKey @param string $relatedPivotKey @param string $parentKey @param string $relatedKey @param string|null $relationName @param bool $inverse @return \Illuminate\Database\Eloquent\Relations\MorphToMany<TRelatedModel, TDeclaringModel>
newMorphToMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function morphedByMany( $related, $name, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null, ) { $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); // For the inverse of the polymorphic many-to-many relations, we will change // the way we determine the foreign and other keys, as it is the opposite // of the morph-to-many method since we're figuring out these inverses. $relatedPivotKey = $relatedPivotKey ?: $name.'_id'; return $this->morphToMany( $related, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation, true, ); }
Define a polymorphic, inverse many-to-many relationship. @template TRelatedModel of \Illuminate\Database\Eloquent\Model @param class-string<TRelatedModel> $related @param string $name @param string|null $table @param string|null $foreignPivotKey @param string|null $relatedPivotKey @param string|null $parentKey @param string|null $relatedKey @param string|null $relation @return \Illuminate\Database\Eloquent\Relations\MorphToMany<TRelatedModel, $this>
morphedByMany
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
protected function guessBelongsToManyRelation() { $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) { return ! in_array( $trace['function'], array_merge(static::$manyMethods, ['guessBelongsToManyRelation']) ); }); return ! is_null($caller) ? $caller['function'] : null; }
Get the relationship name of the belongsToMany relationship. @return string|null
guessBelongsToManyRelation
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT
public function joiningTable($related, $instance = null) { // The joining table name, by convention, is simply the snake cased models // sorted alphabetically and concatenated with an underscore, so we can // just sort the models and join them together to get the table name. $segments = [ $instance ? $instance->joiningTableSegment() : Str::snake(class_basename($related)), $this->joiningTableSegment(), ]; // Now that we have the model names in an array we can just sort them and // use the implode function to join them together with an underscores, // which is typically used by convention within the database system. sort($segments); return strtolower(implode('_', $segments)); }
Get the joining table name for a many-to-many relation. @param string $related @param \Illuminate\Database\Eloquent\Model|null $instance @return string
joiningTable
php
illuminate/database
Eloquent/Concerns/HasRelationships.php
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasRelationships.php
MIT