Laravel 铸造时缺少碳错误数据
Laravel Carbon error data missing with casting
我有一个日期 属性,我尝试在保存期间将其转换为 DateTime 格式。
protected $casts = [
'date' => 'datetime:Y-m-d H:m',
];
在我的控制器中,我有以下方法。
public function change(int $gameSerieId, Request $request)
{
try {
$gameSerie = GameSerie::findOrFail($gameSerieId);
$gameSerie->update($request->all());
return response()->json('ok');
} catch (\Exception $exception) {
throw new GameApiException('Something went wrong!');
}
}
但是,我得到一个错误 "Data Missing" 因为我的日期输入格式看起来像一个字符串:2019-11-17 21:00
.
我发现了对设置属性使用修改器的能力
public function setDateAttribute($date)
{
$this->attributes['date'] = Carbon::make($date);
}
https://laravel.com/docs/5.7/eloquent-mutators#defining-a-mutator
我有一个日期 属性,我尝试在保存期间将其转换为 DateTime 格式。
protected $casts = [
'date' => 'datetime:Y-m-d H:m',
];
在我的控制器中,我有以下方法。
public function change(int $gameSerieId, Request $request)
{
try {
$gameSerie = GameSerie::findOrFail($gameSerieId);
$gameSerie->update($request->all());
return response()->json('ok');
} catch (\Exception $exception) {
throw new GameApiException('Something went wrong!');
}
}
但是,我得到一个错误 "Data Missing" 因为我的日期输入格式看起来像一个字符串:2019-11-17 21:00
.
我发现了对设置属性使用修改器的能力
public function setDateAttribute($date)
{
$this->attributes['date'] = Carbon::make($date);
}
https://laravel.com/docs/5.7/eloquent-mutators#defining-a-mutator