我正在使用Laravel访问器以特定格式访问日期字段的值。但是,我从InvalidArgument
的碳库中得到错误。以下是我的代码。
<?php
namespace App\modules\hrm;
use App\Helper\DateFormat;
use Illuminate\Database\Eloquent\Model;
class Holiday extends Model
{
protected $guarded = [];
protected $dates = ['off_date'];
public function setOffDateAttribute($date)
{
$this->attributes['off_date'] = DateFormat::convertToDBFormat($date);
}
public function getOffDateAttribute($date){
return DateFormat::convertToUIFormat($date);
}
}
帮助文件
<?php
namespace App\Helper;
use Carbon\Carbon;
class DateFormat
{
public static function convertToDBFormat($date) {
return Carbon::createFromFormat('d/m/Y', $date)->format('Y-m-d');
}
public static function convertToUIFormat($date) {
return Carbon::createFromFormat('Y-m-d', $date)->format('d/m/Y');
}
}
?>
模型功能
public function show(Request $request)
{
$data = Holiday::find(json_decode($request->get('data'),true)['id']);
if (empty($data)) {
return response()->json(array("status" => "error", "message" => "Invalid ID"));
} else {
return response()->json(array("status" => "success", "data" => $data));
}
}
我收到来自Carbon的错误,但我认为我在发送这些值时做错了,也许我没有正确使用laravel访问器功能。
错误:
"message": "Trailing data",
"exception": "InvalidArgumentException",