diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 44878f7bf880..af54e4ae42cf 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -1506,43 +1506,48 @@ protected function performDeleteOnModel() /** * Begin querying the model. * + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder */ - public static function query() + public static function query(?array $fetchModes = []) { - return (new static)->newQuery(); + return (new static)->newQuery($fetchModes); } /** * Get a new query builder for the model's table. * + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder */ - public function newQuery() + public function newQuery(?array $fetchModes = []) { - return $this->registerGlobalScopes($this->newQueryWithoutScopes()); + return $this->registerGlobalScopes($this->newQueryWithoutScopes($fetchModes)); } /** * Get a new query builder that doesn't have any global scopes or eager loading. * + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder */ - public function newModelQuery() + public function newModelQuery(?array $fetchModes = []) { return $this->newEloquentBuilder( - $this->newBaseQueryBuilder() + $this->newBaseQueryBuilder(), + $fetchModes )->setModel($this); } /** * Get a new query builder with no relationships loaded. * + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder */ - public function newQueryWithoutRelationships() + public function newQueryWithoutRelationships(?array $fetchModes = []) { - return $this->registerGlobalScopes($this->newModelQuery()); + return $this->registerGlobalScopes($this->newModelQuery($fetchModes)); } /** @@ -1563,11 +1568,12 @@ public function registerGlobalScopes($builder) /** * Get a new query builder that doesn't have any global scopes. * + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder */ - public function newQueryWithoutScopes() + public function newQueryWithoutScopes(?array $fetchModes = []) { - return $this->newModelQuery() + return $this->newModelQuery($fetchModes) ->with($this->with) ->withCount($this->withCount); } @@ -1598,11 +1604,12 @@ public function newQueryForRestoration($ids) * Create a new Eloquent query builder for the model. * * @param \Illuminate\Database\Query\Builder $query + * @param array|null $fetchModes * @return \Illuminate\Database\Eloquent\Builder<*> */ - public function newEloquentBuilder($query) + public function newEloquentBuilder($query, ?array $fetchModes = []) { - return new static::$builder($query); + return new static::$builder($query, fetchUsing: $fetchModes); } /** diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index dd7076c12dce..f235bdd95af5 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -249,13 +249,6 @@ class Builder implements BuilderContract */ public $useWritePdo = false; - /** - * The custom arguments for the PDOStatement::fetchAll / fetch functions. - * - * @var array - */ - public array $fetchUsing = []; - /** * Create a new query builder instance. * @@ -265,6 +258,7 @@ public function __construct( ConnectionInterface $connection, ?Grammar $grammar = null, ?Processor $processor = null, + public ?array $fetchUsing = [], ) { $this->connection = $connection; $this->grammar = $grammar ?: $connection->getQueryGrammar(); diff --git a/tests/Database/DatabaseEloquentDynamicRelationsTest.php b/tests/Database/DatabaseEloquentDynamicRelationsTest.php index 346f00fa2297..28488a5f294d 100644 --- a/tests/Database/DatabaseEloquentDynamicRelationsTest.php +++ b/tests/Database/DatabaseEloquentDynamicRelationsTest.php @@ -101,7 +101,7 @@ public function getResults() // } - public function newQuery() + public function newQuery(?array $fetchModes = []) { $query = new class extends Query {