-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.2] Model casting to any object #11225
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
Conversation
class_exists($this->casts[$key]) || | ||
in_array($key, app()->getBindings()) | ||
) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 newline after opening brace; 2 found
Wouldn't it be better to just have a |
Currently if it's |
I think the ability to extend castAttribute with custom cast types would be very helpful, and perhaps a better route to acheive the functionality you are after and offer greater flexibility for others? If a check was made for a custom attribute cast type before both the check of a null value and the getCastType switch, it would be possible to create custom cast types for objects (including null objects), serialzed objects (which are not currenty supported) and any other implementations (binary, CSV, bson etc.) |
@@ -3,6 +3,7 @@ | |||
use Mockery as m; | |||
use Illuminate\Database\Eloquent\Model; | |||
use Illuminate\Database\Eloquent\Relations\Relation; | |||
use Illuminate\Support\Fluent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please sort by length
👎 |
This involves using the application object that eloquent shouldn't know about. |
Yeah, we probably won't be able to have access to the container from within Eloquent. Probably the only option I can think of is to simply pass the raw database value into the object just using like One thing I'm wondering about is how do you cast back for storage... like if I do:
How do I know what to make |
I appreciate the feedback. I did miss this in the implementation, but my thought was that casted objects would need to implement |
The intended functionality of this PR is to provide the ability to cast to any object. A great example of where this would be useful is user settings when they are stored as json in a
users.settings
column. Having the json converted to an object allows one to easily define the default values for settings and a streamlined method of accessing these settings.Here's an example:
Currently I'm experiencing an issue with a unit test am I'm unsure how to mock
app()
. Thus a test is failing:I'd like to add another assertion or two once I can get passed this issue.