Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Add Model::query($fetchModes) syntactic sugar #54745

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1506,43 +1506,48 @@
/**
* Begin querying the model.
*
* @param array|null $fetchModes
* @return \Illuminate\Database\Eloquent\Builder<static>
*/
public static function query()
public static function query(?array $fetchModes = [])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?array?
Can it be null?

{
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<static>
*/
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<static>
*/
public function newModelQuery()
public function newModelQuery(?array $fetchModes = [])
Copy link

@macropay-solutions macropay-solutions Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change that was avoided in @bert-w 's PR.
We think that PR was ok just that it deleted a function that generated a bug and so the whole feature was reverted.

{
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<static>
*/
public function newQueryWithoutRelationships()
public function newQueryWithoutRelationships(?array $fetchModes = [])
{
return $this->registerGlobalScopes($this->newModelQuery());
return $this->registerGlobalScopes($this->newModelQuery($fetchModes));
}

/**
Expand All @@ -1563,11 +1568,12 @@
/**
* Get a new query builder that doesn't have any global scopes.
*
* @param array|null $fetchModes
* @return \Illuminate\Database\Eloquent\Builder<static>
*/
public function newQueryWithoutScopes()
public function newQueryWithoutScopes(?array $fetchModes = [])
{
return $this->newModelQuery()
return $this->newModelQuery($fetchModes)
->with($this->with)
->withCount($this->withCount);
}
Expand Down Expand Up @@ -1598,11 +1604,12 @@
* 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);

Check failure on line 1612 in src/Illuminate/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / Source Code

Unknown parameter $fetchUsing in call to Illuminate\Database\Eloquent\Builder constructor.
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentDynamicRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getResults()
//
}

public function newQuery()
public function newQuery(?array $fetchModes = [])
{
$query = new class extends Query
{
Expand Down
Loading