Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Hi, take a look at this one, http://laravel.io/bin/mGJEm . I used a closure bind, a php 5.4.x feature http://php.net/manual/en/closure.bind.php . You can integrate it to your trait etc.. Also, it needs improvement since converting your model to array using toArray() wont get the mutated value. Let me know if this works for you.

0

@eckenroed, maybe you could go a bit deeper and do automatic date conversions by overriding fromDateTime and asDateTime functions of default Eloquent Model class. I haven't tried it personally, just looks like a good point to tackle this issue.

0

2 great ideas. I hadn't thought about toAray(). I'm determined to make this work. Once I do I'll publish the final result (including middleware and JavaScript) so that others don't have to re-invent the wheel.

0

Well, I made some progress on this..but I'm not sure if I like the cost.

I still have to work on the toArray() which I think I can accomplish...however, in order to accomplish it I would have to do what I did to get to this point...which is to override core functions.

Essentially I copied the fromDateTime() and asDateTime() function from Illuminate\Database\Eloquent\Model and put them in my trait to allow them to override the existing method.

All I did was add a few lines to set the timezone for outgoing dates and convert it back to UTC for incoming dates. So this covers all dates that are marked both in the $dates array and known dates.

Is that bad practice? To override a core method like that? I feel like it might be, but it solves my problem so (pardon the phrase) eloquently.

I found the toArray() function and I can do the same to it so that it provides dates in the current users time when casting to an array. So I may do that just so I can use it and cross my fingers that there are not future changes to the core versions of the methods...?

Here's the updated trait: http://laravel.io/bin/LkNad

0

##Final Working version of Trait

So I got the trait working on multiple models to always output the time in the current timezone and to store it in UTC back in the database. I had to copy and paste the toArray() function from Illuminate\Database\Eloquent\Model and throw it in the trait. This is so that when it calls the asDateTime and fromDateTime method it calls the ones in the trait and not the ones in the class.

Again, I have to override the original functions, and again it assumes you always want UTC in your database but it works. If you wanted to store in another timezone you could add an Environmental or Config option and then call that instead of "UTC" in the trait.

I plan on putting together an entire package of stuff for people that want to implement something like this (since I see it all over google with really no easily workable solution).

My solution requires you to include a few javascript files...include a middleware file...and include a trait on the models you want it to work on and that's it.

Final Trait: http://laravel.io/bin/32MY4

0

In case anybody wants to see the full final package I am using I put it on GitHub. It's my first public code to share...so be gentle if you find problems.

https://github.com/eckenroed/LaravelTimezone

0

If you want another backup software then try Ahsay Software. Ahsay Backup Software is equipped with various speed boosting technologies, including multi-threading, In-File Delta block level incremental backup, server side delta merging, etc. that can greatly enhance the backup and restore speed. You can try this software too.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

eckenroed eckenroed Joined 28 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.