51 model json encodemodel returns and ide loses type after where first#52
Merged
benkhalife merged 2 commits intoJun 11, 2026
Conversation
Model did not implement JsonSerializable. PHP serializes only public
properties on plain objects, so json_encode($model) produced {}.
toArray() and toJson() were unaffected but any code passing a model
directly to json_encode or inside an array response was silently broken.
Add implements JsonSerializable and jsonSerialize() which delegates
to toArray() — hidden fields, casts, and loaded relations all apply.
Builder::first() is typed as object|false, so User::where(...)->first() gave the IDE no information about the returned type. Autocompletion and static analysis were lost after any where() call. Add ModelBuilder — a thin decorator around Builder that re-declares terminal methods (first, find, get, paginate, count, etc.) with model-aware return types. Model::query() and Model::where() now return ModelBuilder<static> so the IDE knows the result is always the concrete model class. All chainable Builder methods (where, orderBy, limit, join, etc.) are forwarded transparently via __call so existing query chains are unaffected. Model::with() continues to pass a raw Builder to EagerBuilder to avoid a type conflict. Adds regression tests covering: - json_encode($model) produces correct non-empty output - hidden fields are excluded from json_encode output - where()->first() returns model instance, null when no match - return ['user' => $model] encodes correctly in API responses Fixed #51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.