Laravel Eloquent Carbon Error以Postgresql时间戳格式跟踪数据(毫秒)

时间:2019-04-10 06:20:23

标签: laravel postgresql eloquent timestamp php-carbon

我正在使用Laravel 5.7和Postgresql作为数据库。出现了严重错误的问题:碳尾随数据,由行数据上具有毫秒数的created_at / updated_at引起。

我从另一个线程尝试过此解决方案: Laravel model Trailing Data when save the model 但是出现错误,“数据丢失”。

这是模型的一个示例,该示例具有created_at / updated_at字段:

class Department extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'hr_department';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'message_main_attachment_id',
        'name',
        'complete_name',
        'active',
        'company_id',
        'parent_id',
        'manager_id',
        'note',
        'color',
        'create_uid',
        'create_date',
        'write_uid',
        'write_date',
    ];

    const CREATED_AT = 'create_date';
    const UPDATED_AT = 'write_date';

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'create_date' => 'datetime',
        'write_date' => 'datetime',
    ];

    public function DepartmentParent() {
        return $this->belongsTo('App\Department', 'parent_id');
    }

    public function getDepartmentParentNameAttribute() {
        if ($this->DepartmentParent) {
            return $this->attributes['department_parent_name'] = $this->DepartmentParent->name;
        }

        return null;
    }
}

控制器中显示api的json结果的动作:

/**
     * Display a listing of the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function list(Request $request)
    {
        $httpCode = 200;
        $message = "Here is the department list";
        $modelData = null;

        try {
            $query = Department::whereRaw('0 = 0');
            if ($request->active)
                $query = $query->where('active', $request->active);
            if ($request->name)
                $query = $query->where('name', $request->name);
            $query = $this->setRestFilter($query, $request);
            $modelData = $query->get();
        } catch (Exception $e) {
            $httpCode = $e->getCode();
            $message = $e->getMessage();
        }

        return response()->json([
            'message' => $message,
            'modelData' => $modelData
        ], $httpCode);
    }

是否有任何配置,因此碳无法从数据库中读取毫秒?还是需要防止laravel在created_at / updated_at字段中保存毫秒数?以及如何在全球范围内做到这一点?

0 个答案:

没有答案