-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Adds Model::casts()
method and named static methods for built-in casters
#47237
Conversation
I'm not so sure about that, it totally sounds like a possible relation name. In the previous PR, that was my main concern with the idea of having a method for allowing named constructors. Maybe we can check the return type of the That point put apart, LGTM. I'm really looking forward to the array syntax. 👍 |
@innocenzi I'm not sure if a breaking change on The |
@taylorotwell I see, interesting. Seems like we can go forward with |
Co-authored-by: Enzo Innocenzi <enzo@innocenzi.dev>
7d227a8
to
e812be5
Compare
Model::casts()
method and named static methods for built-in castersModel::casts()
method and named static methods for built-in casters
@taylorotwell Rebased to Laravel 11.x. |
Using laravel/framework v10.48.9 protected function casts(): array
{
return [
'starts_at' => 'datetime',
];
} Then my php artisan tinker
MyModel::first()->starts_at
= "2024-04-24 21:19:09" But if I use protected $casts = [
'starts_at' => 'datetime',
]; Then everythig works as expected: php artisan tinker
MyModel::first()->starts_at
= Illuminate\Support\Carbon @1713993549 {#5208
date: 2024-04-24 21:19:09.0 UTC (+00:00),
} Am I doing something wrong? |
@oleynikd this was merged into 11.x not 10.x |
Depends on: #47235.
This pull request is originally created from ideas of #46649, does not have any breaking change, and it enables the usage of an new
Model::casts()
method and named static methods for built-in casters.Note that, by enabling the
casts()
method, developers can easily use named static methods on the built-in casters. In addition, they may add their own named static methods to their own casters if they want. As syntax, just likeAsCollection::using(OptionCollection::class)
is not supported when using properties.For convenience, support for "array" syntax, on the property and method, was also added on this pull request, so developers can also use this syntax:
Finally, some technical details: as already mentioned, this pull request does not introduce any breaking changes. Also, there are no performance drawbacks as the merging of the method and property occurs during class instantiation.
In addition, when an attribute is defined both on the property and method, the method one takes priority.