[13.x] Add fluent dump methods to Eloquent models#60296
Closed
andreapozza wants to merge 2 commits into
Closed
Conversation
Member
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
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.
Summary
This PR introduces fluent
dump()anddd()debugging methods directly to Eloquent models by leveraging theIlluminate\Support\Traits\Dumpabletrait.This change leaves the static behavior entirely unchanged: calling
User::dump()orUser::dd()will continue to be forwarded to the Query Builder as before.Why this PR? (Fixing Unexpected Behavior)
The main driver behind this PR is addressing an unexpected behavior in the developer experience when debugging hydrated models.
Currently, the Eloquent Query Builder is fully inspectable mid-chain. However, if a developer attempts to fluently call
->dump()or->dd()on a hydrated model instance, the call is caught by the magic__call()method and forwarded down to a new Query Builder instance. Instead of inspecting the model's current state, the developer unexpectedly gets a dump of the Query Builder itself.By adding the
Dumpabletrait directly to theModelclass, we resolve this friction:->dump()on an instantiated model now actually dumps that specific model instance (its attributes, relations, and mutated state) rather than routing back to the builder.The Alias Trick (
dump as protected)To achieve this without breaking backwards compatibility or creating conflicts with the magic
__callrouting, this PR utilizes an aliasing trick when importing the trait:By aliasing the trait's methods as
protected, we prevent them from hiding or overriding the public static/magic behavior blindly. Instead, it allows us to intercept the calls explicitly inside__call()and__callStatic(). This ensures that:__call(), match the array check, and safely execute the protected trait method internally on$this.__callStatic(). It is explicitly intercepted by thein_arraycheck and safely forwarded to a new Query Builder instance (static::query()->$method(...)). This ensures that the static behavior remains completely untouched and backward-compatible.Usage Example