显示错误的“无效日期价值”在Laravel 7中导入Excel文件时
我想从Excel文件导入数据。在插入数据时,我想根据请求的Excel文件计算一些值。但是,时间差异不是返回正确的日期格式。
控制器:
public function import(Request $request){
$request->validate([
'import_file'=>'required'
]);
Excel::import(new ImportAttendanceExcel,request()->file('import_file'));
return back()->with('message','XL File Import Successfully!');
}
模型:
class ImportAttendanceExcel implements ToModel, WithStartRow
{
public function model(array $row)
{ $id = 1;
$settings = Setting::find($id);
$open = Carbon::parse($settings->opening_time);
$end = Carbon::parse($settings->ending_time);
$date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
$user = $row[0];
$exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
if($row[0] != null){
if(empty($exist_attd)){
$attendances = new Attendance;
$attendances->user_id = $row[0];
$attendances->created_by = Auth::user()->id;
$attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
$attendances->attendance_status = $row[3];
$attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
$attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
$csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
$csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
if( $csv_check_in > $open){
$late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
$attendances->late = $late_count;
}
if( $csv_check_out < $end){
$early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
$attendances->early = $early_count;
}
$attendances->save();
}
}
}
public function startRow(): int
{
return 2;
}
}
带错误的返回值:
(SQL: insert into `attendances` (`user_id`, `created_by`, `attendance_date`, `attendance_status`, `check_in`, `check_out`, `early`, `updated_at`, `created_at`) values (2, 1, 44713, 1, 0.42361111111111, 0.77430555555556, 23:59:59, 2022-06-14 15:12:47, 2022-06-14 15:12:47))
Excel文件:
ID | 日期 | 状态 | 检查 | 结帐 |
---|---|---|---|---|
1 | 2022-06-10 | 1 | 10:15:00 | 18:00:00 |
这里的日期格式和计算无法正常工作。我该如何解决?
I want to import data from an Excel file. While inserting data, I want to calculate some value depending on the requested Excel file. But the time difference is not returning the correct date format.
Controller:
public function import(Request $request){
$request->validate([
'import_file'=>'required'
]);
Excel::import(new ImportAttendanceExcel,request()->file('import_file'));
return back()->with('message','XL File Import Successfully!');
}
Model:
class ImportAttendanceExcel implements ToModel, WithStartRow
{
public function model(array $row)
{ $id = 1;
$settings = Setting::find($id);
$open = Carbon::parse($settings->opening_time);
$end = Carbon::parse($settings->ending_time);
$date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
$user = $row[0];
$exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
if($row[0] != null){
if(empty($exist_attd)){
$attendances = new Attendance;
$attendances->user_id = $row[0];
$attendances->created_by = Auth::user()->id;
$attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
$attendances->attendance_status = $row[3];
$attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
$attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
$csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
$csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
if( $csv_check_in > $open){
$late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
$attendances->late = $late_count;
}
if( $csv_check_out < $end){
$early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
$attendances->early = $early_count;
}
$attendances->save();
}
}
}
public function startRow(): int
{
return 2;
}
}
Return Value with error:
(SQL: insert into `attendances` (`user_id`, `created_by`, `attendance_date`, `attendance_status`, `check_in`, `check_out`, `early`, `updated_at`, `created_at`) values (2, 1, 44713, 1, 0.42361111111111, 0.77430555555556, 23:59:59, 2022-06-14 15:12:47, 2022-06-14 15:12:47))
Excel File:
id | date | status | check-in | check-out |
---|---|---|---|---|
1 | 2022-06-10 | 1 | 10:15:00 | 18:00:00 |
Here the date format and the calculation is not working properly. How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能直接将日期从Excel投入到DB。您需要先格式化它。由于Laravel-Excel是由phpspreadsheet构建的。您可以
从 phpspreadsheet文档:
You cant just directly throw date from excel to DB. You need to format it first. Since laravel-excel is built from phpSpreadsheet. You can import Date Class
From the PHPSpreadsheet docs:
我的问题得到了解决:
My problem is solved by this :