Laravel – convert time to user’s timezone in model

I have the following model that returns data from Dynamo AWS, my data has a time in EPOCH, and I convert it to the formatted date as below:

class File extends BaseDynamo
{
    protected $table = 'Dados';
    protected $primaryKey = 'SUI';
    protected $sortKey = ['EPOCH','desc'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'EPOCH' => 'datetime:Y-m-d H:i:s',
    ];
}

However, the time returning does not match the time that the user is seeing the data and that is why I would like to make the conversion using the registration timezone as an extra field in the user:
private function timezone(){
    $user = User::find(Auth::id());
    $extra_field = ExtraField::where('description','=','TimeZone')->first();

    if($extra_field) {
        $timezone = ExtraFieldValue::where('extra_field_id', '=', $extra_field->id)->where('user_id', '=', $user->id)->first()->value;
    } else {
        $timezone = 'America/Sao_Paulo';
    }

    return $timezone;
}

I tried to do something like this:
UPDATE
class File extends BaseDynamo
{
    protected $table = 'Dados';
    protected $primaryKey = 'SUI';
    protected $sortKey = ['EPOCH','desc'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'EPOCH' => 'datetime:Y-m-d H:i:s',
    ];

    public function getEpochAttribute($value)
    {
        $user = User::find(Auth::id());
        $extra_field = ExtraField::where('description','=','TimeZone')->first();

        if($extra_field) {
            $timezone = ExtraFieldValue::where('extra_field_id', '=', $extra_field->id)->where('user_id', '=', $user->id)->first()->value;
        } else {
            $timezone = 'America/Sao_Paulo';
        }
        Log::info('timezone');
        Log::info($value);
        Log::info($timezone);
        Log::info($value->setTimezone($timezone)->toDateTimeString("Y-m-d H:i:s"));

        return $value->setTimezone($timezone)->toDateTimeString("Y-m-d H:i:s");
    }
}

It’s not even calling my function, when I do this:
$collection = collect(File::filterBetween('EPOCH', $date)->filter('SUI','=',$asset->mac_id)->scan());
$my_datetime = $collection->whereIn($variable->key, [0,1])->pluck('EPOCH')->last();

But it doesn’t work, can someone help me how to make this conversion?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I would say read more about Accessors and Mutators

Would be something like

public function getEPOCHAttribute($value)
{
    //Logic to change the date
    return $value;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x