具有时区的Postgres + Laravel时间戳

时间:2018-07-13 02:25:55

标签: php postgresql laravel-5 eloquent timestamp-with-timezone

我有一个如下的父类,它扩展了Illuminate \ Database \ Eloquent \ Model,然后将其扩展到Laravel 5.6中的模型。我有日期列,可以使用$table->timestampsTz(); $table->softDeletesTz();来存储带有时区的时间戳,但是我可以通过修补程序来获取记录,但是当我在浏览器中查看记录时,我猜想日期序列化是完全错误的,最终导致此错误:

InvalidArgumentException
Unexpected data found.

 InvalidArgumentException
…/vendor/nesbot/carbon/src/Carbon/Carbon.php 775 

我想念什么?

class BaseModel extends EloquentModel {

    use SoftDeletes;

    protected $primaryKey = 'id';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    protected $dateFormat = 'Y-m-d H:i:s.u';

    public function getDateFormat() {
        return 'Y-m-d H:i:s.u';
    }

    private function mutateTimestampValue($timestamp) {
        return \Carbon\Carbon::parse($timestamp)->timezone( config('app.timezone') )->format('c');
    }

    public function getCreatedAtAttribute($value) {

        return $this->mutateTimestampValue($value);
    }

    public function getUpdatedAtAttribute($value) {

        return $this->mutateTimestampValue($value);
    }

    public function getDeletedAtAttribute($value) {

        return $this->mutateTimestampValue($value);
    }

    protected static function boot() {
        parent::boot();
        self::creating(function ($model) {
            $model->{$model->getKeyName()} = (string) Str::orderedUuid();
        });
    }
}

0 个答案:

没有答案