2

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?

Marcius Leandro
  • 775
  • 1
  • 11
  • 34
  • 2
    1) That function would not be called because it's inside of single quotes. Functions inside of single quotes are just strings, functions inside of double-quotes could throw errors. Concatenation would be the way to go BUT 2) Class property initialization can only be done with simple values, not functions. – aynber Sep 03 '21 at 15:16
  • @aynber any suggestions on how to do this then? Can I do something in the middle of authenticate that would be valid for the entire section of my user? – Marcius Leandro Sep 03 '21 at 15:25
  • 2
    I'd suggest checking out https://stackoverflow.com/questions/43589850/timezone-middleware-does-not-work and https://stackoverflow.com/questions/50768490/display-all-dates-on-models-in-the-user-s-timezone-laravel – aynber Sep 03 '21 at 15:31

1 Answers1

4

I would say read more about Accessors and Mutators

Would be something like

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