[5.5] [WIP] Eloquent object casting#18106
Closed
tillkruss wants to merge 2 commits intolaravel:masterfrom
Closed
Conversation
8b9765d to
1dcfce2
Compare
bad0d42 to
98bf555
Compare
98bf555 to
3dce9b9
Compare
Contributor
|
You know I had some thoughts on this, but they were valueless and this was locked so I forgot them. |
Contributor
|
I like this PR and would love to see it in the core. But a small adjustment would be great. Adding a fourth And would it be an idea to return another value than an instance of the casting class? For example a PHP primitive or any other class. So use the casting class only as caster and not as data wrapper. |
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.
This is a direct port Taylor's PR (#13706). Related "Internals" discussion is: laravel/ideas#9.
Object casting solves often requested features like:
$encryptsproperty that encrypts model attributes #16457, [5.3] Offer an "Attribute Casting" of encrypt #16443)polygon,enum,cidr,macaddrObjects need to implement two methods (all 3 parameters are optional):
The method signatures are slightly different to #13706, because this PR passes the attribute key to both methods, in order to make objects reusable (so attribute keys don't have to be hard coded).
Objects can be set/updated as usual:
This however does not work:
You may of course add methods to objects:
User.phpToken.phpThe token example touches a single model attributes and acts as a accessor/mutator.
Address.phpThe address example touches multiple model attributes and has a
validate()method for extra functionality.Json.phpThe JSON example acts as a little helper to make it easier work with nested JSON data.