在laravel中,db在postgres中。用户表中存储着created_at
和updated_at。这两列均具有时间戳记,其中时区为数据类型。因此,在默认情况下存储数据时,postgres会存储带有时区的默认时间戳。但是,当尝试使用->toArray()
访问created_at或将获取的数据转换为数组时,它将给出
InvalidArgumentException {#312 ▼
#message: "Trailing data"
#code: 0
#file: "/var/www/html/wallet-admin/vendor/nesbot/carbon/src/Carbon/Carbon.php"
#line: 910
trace: {▶}
}
我已经尝试过以用户模式设置日期格式
protected $dateFormat = 'Y-m-d H:i:s';
但是当我尝试更新此模型数据时,它也会给出上述错误。
protected $dateFormat = 'Y-m-d H:i:s';
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function getDateFormat()
{
return 'U';
}
public function getCreatedAtAttribute($attr){
return Carbon::parse($attr)->format('Y-m-d H:i:s');
}
$user = User::select('*')->where('id',$id)->first();
//dd($user->toArray()); // This also give above error
$user->first_name = trim($request->first_name);
$user->last_name = trim($request->last_name);
$user->username = trim($request->username);
$user->email = trim($request->email);
$user->user_type = $request->user_type;
$user->status = isset($request->status)?1:0;
$user->save();
由于上述结果,数据未更新。请帮忙。谢谢