我創(chuàng)建了 Excel 導(dǎo)入函數(shù),它工作正常,直到我注意到日期沒有正確插入,所以我嘗試格式化日期,就像Y-m-d在 MySql 中正確存儲(chǔ)一樣,但 Carbon 給出了以下錯(cuò)誤Carbon\Exceptions\InvalidFormatException發(fā)現(xiàn)意外數(shù)據(jù)。發(fā)現(xiàn)意外數(shù)據(jù)。追蹤數(shù)據(jù)在Excel中我可能有d/m/Y或者Y/m/d所以我想在數(shù)據(jù)庫中格式化為tored我的導(dǎo)入代碼public function model(array $row){ return new Staff([ 'employee_no' => $row['id'], 'name' => $row['name'], 'address' => $row['address'], 'fathers_name' => $row['father'], 'dob' => $this->transformDate($row['dob']), 'blood_group' => $row['blood_group'], 'phone' => $row['phone'], 'password' => Hash::make($row['id']), ]);}public function transformDate($value, $format = 'Y-m-d'){ try { return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value)); } catch (\ErrorException $e) { return \Carbon\Carbon::createFromFormat($format, $value); }}我的員工模型class Staff extends Authenticatable{ protected $fillable = [ 'name', 'employee_no', 'designation_id', 'fathers_name', 'dob', 'identification_mark', 'blood_group', 'phone', 'address', 'height', 'rfid_no', 'building_id', 'password', ]; protected $casts = ['dob'];}正確的方法是什么,還是我必須采用新的format方法dob
1 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
Carbon::createFromFormat($format, $time)
應(yīng)該接收給定的格式$time
,而不是您想要轉(zhuǎn)換成的格式。嘗試將其更改為$format = 'd/m/Y'
,正如您所說,這是您期望從記錄中獲得的格式。
之后,您只需在 Carbon 對(duì)象上創(chuàng)建->toDateString()
或即可獲取數(shù)據(jù)庫格式。->toDateTimeString()
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)
0/150
提交
取消