I'm using Laravel Livewire v2 and I need to bind a form date input to a model Carbon attribute in the backend Livewire component, but it seems to be impossible due date formatting.
To be more precise:
- when selecting a date the form date input picker livewire make a api call sending the correct date (e.g.:
2020-02-02
) - the Livewire component correctly receive and pass the string value to the model
- the model cast the string to a Carbon object and assign it to its fillable attribute
- the Livewire component try to send back to the client the data but it doesn't perform a
->format('Y-m-d')
format on the Carbon object, so a serialized date object is sent - when object is unserialized fronted receive e.g.
2012-12-25T20:30:00.000000+04:00 MSK
- the date input break
I also found that in Livewire v1 it was possible to cast properties (link) using key-value array or custom casting methods, but this has been removed in v2 (link); there is an example with hydrate
/dehydrate
methods to achieve the same effect but the example cover the case of a component property, not a nested one as in my case.
Is there a way to cast a model date prop or to add a format when sending data back to client?