Automatic timezone conversion in Eloquent? #53629
Unanswered
redelschaap
asked this question in
Ideas
Replies: 2 comments 12 replies
-
When you use form requests, you can do class MyFormRequest
{
public function rules(): array
{
return [
// …
];
}
public function passedValidation()
{
$this->merge([
'publishedAt' => $this->date('publishedAt')->setTimezone(config('app.timezone')),
]);
}
} I know, it's not a solution, but it can be a workaround, especially if you use this in a base class. |
Beta Was this translation helpful? Give feedback.
9 replies
-
In a Joomla plugin I used to use a decade or so ago, timezone handling was one of the biggest cause of issues and the biggest headache for developers to get right. In my opinion:
I haven't got the time to check whether these functions are already provided in Carbon or Laravel. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am developing an application that has an API where consumers can send requests containing dates. They would post these dates in ISO format, containing a timezone reference. Currently, we have to do a conversion ourselves to the application's timezone (typically UTC). The framework does not convert the timezone when storing dates into the database. To be honest, I would expect this kind of magic from Laravel.
Let's imagine a model with a date cast:
When inserting a model from an API request that accepts an ISO formatted datetime string, we need to store this as follows:
It would be very nice, and would feel much more like Laravel, when we can leave out the
setTimezone()
part.Now, I know that this has already been discussed before and until now the message was that this behavior was intended. But with all the good magic Laravel does on other parts of the framework, it really feels weird that It lacks this kind of magic. At least for me, and given the number of questions about this, also for many other developers.
Would the framework be open for automatic timezone conversion? Perhaps in the next major release, since this will probably break some applications. Although it should only break things when developers currently adjust time or anticipate on this behavior in another way than setting the timezone themselves. I can also think of an option where this would be an opt-in or opt-out function, just like with the
preventLazyLoading
orpreventSilentlyDiscardingAttributes
features.I am willing to write a PR, but only when it has a chance of getting accepted of course. Please let me know!
Beta Was this translation helpful? Give feedback.
All reactions