Skip to content

[12.x] Specify that the query builder returns instances of stdClass#58150

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
axlon:query-builder-types
Dec 17, 2025
Merged

[12.x] Specify that the query builder returns instances of stdClass#58150
taylorotwell merged 1 commit into
laravel:12.xfrom
axlon:query-builder-types

Conversation

@axlon

@axlon axlon commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

The query builder returns query results as instances of stdClass, however currently it is documented as if it can return any object. This PR narrows the type to more accurately reflect what is being returned. This doesn't affect calling code, editors, etc. but it does change something for PHPStan.

PHPStan has a special list of classes on which it assumes any property can exist, it calls this universal object crates, stdClass is one of these classes, this means that:

Before:

$result = $query->find($id); // <- object|null

if (is_null($result)) {
    return;
}

echo "Hello, $result->first_name"; // <- PHPStan error, first_name might not exist on 'object'

After:

$result = $query->find($id); // <- stdClass|null

if (is_null($result)) {
    return;
}

echo "Hello, $result->first_name"; // <- OK

Relevant PHPStan docs: https://phpstan.org/config-reference#universal-object-crates

Note: Some query builder methods (get, cursor) already use stdClass

@axlon

axlon commented Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

Failing test seems unrelated (this PR only changes docs)

@taylorotwell taylorotwell merged commit 735778d into laravel:12.x Dec 17, 2025
70 of 72 checks passed
iam-subho pushed a commit to iam-subho/laravel-core that referenced this pull request Dec 18, 2025
calebdw added a commit to calebdw/larastan that referenced this pull request Dec 24, 2025
akyrey pushed a commit to akyrey/framework that referenced this pull request Dec 29, 2025
@axlon axlon deleted the query-builder-types branch March 4, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants