his */ public function withAttributes(Expression|array|string $attributes, $value = null, $asConditions = true) { if (! is_array($attributes)) { $attributes = [$attributes => $value]; } if ($asConditions) { foreach ($attributes as $column => $value) { $this->where($this->qualifyColumn($column), $value); } } $this->pendingAttributes = array_merge($this->pendingAttributes, $attributes); return $this; } /** * Apply query-time casts to the model instance. * * @param array $casts * @return $this */ public function withCasts($casts) { $this->model->mergeCasts($casts); return $this; } /** * Execute the given Closure within a transaction savepoint if needed. * * @template TModelValue * * @param \Closure(): TModelValue $scope * @return TModelValue */ public function withSavepointIfNeeded(Closure $scope): mixed { return $this->getQuery()->getConnection()->transactionLevel() > 0 ? $this->getQuery()->getConnection()->transaction($scope) : $scope(); } /** * Get the Eloquent builder instances that are used in the union of the query. * * @return \Illuminate\Support\Collection */ protected function getUnionBuilders() { return isset($this->query->unions) ? (new BaseCollection($this->query->unions))->pluck('query') : new BaseCollection; } /** * Get the underlying query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function getQuery() { return $this->query; } /** * Set the underlying query builder instance. * * @param \Illuminate\Database\Query\Builder $query * @return $this */ public function setQuery($query) { $this->query = $query; return $this; } /** * Get a base query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function toBase() { return $this->applyScopes()->getQuery(); } /** * Get the relationships being eagerly loaded. * * @return array */ public function getEagerLoads() { return $this->eagerLoad; } /** * Set the relationships being eagerly loaded. * * @param array $eagerLoad * @return $this */ public function setEagerLoads(array $eagerLoad) { $this->eagerLoad = $eagerLoad; return $this; } /** * Indicate that the given relationships should not be eagerly loaded. * * @param array $relations * @return $this */ public function withoutEagerLoad(array $relations) { $relations = array_diff(array_keys($this->model->getRelations()), $relations); return $this->with($relations); } /** * Flush the relationships being eagerly loaded. * * @return $this */ public function withoutEagerLoads() { return $this->setEagerLoads([]); } /** * Get the "limit" value from the query or null if it's not set. * * @return mixed */ public function getLimit() { return $this->query->getLimit(); } /** * Get the "offset" value from the query or null if it's not set. * * @return mixed */ public function getOffset() { return $this->query->getOffset(); } /** * Get the default key name of the table. * * @return string */ protected function defaultKeyName() { return $this->getModel()->getKeyName(); } /** * Get the model instance being queried. * * @return TModel */ public function getModel() { return $this->model; } /** * Set a model instance for the model being queried. * * @template TModelNew of \Illuminate\Database\Eloquent\Model * * @param TModelNew $model * @return static */ public function setModel(Model $model) { $this->model = $model; $this->query->from($model->getTable()); return $this; } /** * Qualify the given column name by the model's table. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return string */ public function qualifyColumn($column) { $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; return $this->model->qualifyColumn($column); } /** * Qualify the given columns with the model's table. * * @param array|\Illuminate\Contracts\Database\Query\Expression $columns * @return array */ public function qualifyColumns($columns) { return $this->model->qualifyColumns($columns); } /** * Get the given macro by name. * * @param string $name * @return \Closure */ public function getMacro($name) { return Arr::get($this->localMacros, $name); } /** * Checks if a macro is registered. * * @param string $name * @return bool */ public function hasMacro($name) { return isset($this->localMacros[$name]); } /** * Get the given global macro by name. * * @param string $name * @return \Closure */ public static function getGlobalMacro($name) { return Arr::get(static::$macros, $name); } /** * Checks if a global macro is registered. * * @param string $name * @return bool */ public static function hasGlobalMacro($name) { return isset(static::$macros[$name]); } /** * Dynamically access builder proxies. * * @param string $key * @return mixed * * @throws \Exception */ public function __get($key) { if (in_array($key, ['orWhere', 'whereNot', 'orWhereNot'])) { return new HigherOrderBuilderProxy($this, $key); } if (in_array($key, $this->propertyPassthru)) { return $this->toBase()->{$key}; } throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance."); } /** * Dynamically handle calls into the query instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if ($method === 'macro') { $this->localMacros[$parameters[0]] = $parameters[1]; return; } if ($this->hasMacro($method)) { array_unshift($parameters, $this); return $this->localMacros[$method](...$parameters); } if (static::hasGlobalMacro($method)) { $callable = static::$macros[$method]; if ($callable instanceof Closure) { $callable = $callable->bindTo($this, static::class); } return $callable(...$parameters); } if ($this->hasNamedScope($method)) { return $this->callNamedScope($method, $parameters); } if (in_array(strtolower($method), $this->passthru)) { return $this->toBase()->{$method}(...$parameters); } $this->forwardCallTo($this->query, $method, $parameters); return $this; } /** * Dynamically handle calls into the query instance. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public static function __callStatic($method, $parameters) { if ($method === 'macro') { static::$macros[$parameters[0]] = $parameters[1]; return; } if ($method === 'mixin') { return static::registerMixin($parameters[0], $parameters[1] ?? true); } if (! static::hasGlobalMacro($method)) { static::throwBadMethodCallException($method); } $callable = static::$macros[$method]; if ($callable instanceof Closure) { $callable = $callable->bindTo(null, static::class); } return $callable(...$parameters); } /** * Register the given mixin with the builder. * * @param string $mixin * @param bool $replace * @return void */ protected static function registerMixin($mixin, $replace) { $methods = (new ReflectionClass($mixin))->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED ); foreach ($methods as $method) { if ($replace || ! static::hasGlobalMacro($method->name)) { static::macro($method->name, $method->invoke($mixin)); } } } /** * Clone the Eloquent query builder. * * @return static */ public function clone() { return clone $this; } /** * Register a closure to be invoked on a clone. * * @param \Closure $callback * @return $this */ public function onClone(Closure $callback) { $this->onCloneCallbacks[] = $callback; return $this; } /** * Force a clone of the underlying query builder when cloning. * * @return void */ public function __clone() { $this->query = clone $this->query; foreach ($this->onCloneCallbacks as $onCloneCallback) { $onCloneCallback($this); } } }