提交考勤数据出现500内部服务器错误,请求排查问题
考勤数据提交500内部错误排查与修复
问题描述
提交考勤数据时,切换GET/POST路由后仍出现500错误:
POST http://localhost/sip/ess/public/attendance/savepresence 500 (Internal Server Error)
涉及代码如下:
路由代码
Route::post('savepresence', 'Attendance@savepresence')->name('attendance.savepresence');
控制器代码
public function savepresence(Request $req) { $row = Attendance::find($req->input('ap_id')); $rowEmp = Employee::find($row->emp_id); //SO 26-02-2023 Att RAW In $ar_time_in = substr($req->input('ap_in'),11,8); $ar_time_in_old = substr($row->ap_in,11,8); if (!empty($req->input('ap_in')) && $ar_time_in != $ar_time_in_old){ Attendance_raw_tmp::create([ 'emp_id' => $row->emp_id, 'ar_date_tmp' => $thisDate, 'ar_id_tmp' => $rowEmp->emp_code, 'ar_time_tmp' => $ar_time_in, 'ar_lat' => '-6.3013773', 'ar_long' => '106.8429748', 'ar_client_tmp' => $req->input('ar_client_tmp'), 'ar_notes_tmp' => $req->input('ar_notes_tmp'), 'ar_opid' => $this->_user->user_id ]); } //SO 26-07-2022 Att RAW Out $ar_time_out = substr($req->input('ap_out'),11,8); $ar_time_out_old = substr($row->ap_out,11,8); if (!empty($req->input('ap_out')) && $ar_time_out != $ar_time_out_old){ Attendance_raw_tmp::create([ 'emp_id' => $row->emp_id, 'ar_date_tmp' => $thisDate, 'ar_id_tmp' => $rowEmp->emp_code, 'ar_time_tmp' => $ar_time_out_old, 'ar_lat' => '-6.3013773', 'ar_long' => '106.8429748', 'ar_client_tmp' => $req->input('ar_client_tmp'), 'ar_notes_tmp' => $req->input('ar_notes_tmp'), 'ar_opid' => $this->_user->user_id ]); } return redirect()->route('attendance.index')->with('notif', [ 'type' => 'success', 'message' => 'Update Attendance Submission has been saved.' ]); }
视图代码
<div class="container attendancereview-form"> <form action="{{ route('attendance.savepresence') }}" method="post" id="attendancereview-form" class="form-horizontal"> @csrf <input type="hidden" name="ap_id" value="{{ $row->ap_id }}"> <input type="hidden" name="emp_id" value="{{ $row->emp_id }}"> <div class="row"> <div class="col-md-12"> <div class="form-group mb-4 row"> <label for="ap_date" class="col-sm-3 col-form-label text-md-right">Date</label> <div class="col-sm-9"> <input class="form-control" name="ap_date" type="text" autocomplete="off" value="{{ $row->ap_date }}" placeholder="yyyy-mm-dd hh:mm:ss" disabled> </div> </div> <div class="form-group mb-4 row"> <label for="ap_in" class="col-sm-3 col-form-label text-md-right">Time In </label> <div class="col-sm-9"> <input class="form-control" name="ap_in" type="text" autocomplete="off" value="{{ $row->ap_in }}" placeholder="yyyy-mm-dd hh:mm:ss"> <font color="red">example : {{ $row->ap_date }} 08:00:00 </font> </div> </div> <div class="form-group mb-4 row"> <label for="ap_out" class="col-sm-3 col-form-label text-md-right">Time Out </label> <div class="col-sm-9"> <input class="form-control" name="ap_out" type="text" autocomplete="off" value="{{ $row->ap_out }}" placeholder="yyyy-mm-dd hh:mm:ss"> <font color="red">example : {{ $row->ap_date }} 17:00:00 </font> </div> </div> <div class="form-group mb-4 row"> <label for="client" class="col-sm-3 col-form-label text-md-right">Client </label> <div class="col-sm-9"> <input class="form-control" name="ar_client_tmp" type="text" autocomplete="off" value=""> </div> </div> <div class="form-group mb-4 row"> <label for="notes" class="col-sm-3 col-form-label text-md-right">Notes </label> <div class="col-sm-9"> <input class="form-control" name="ar_notes_tmp" type="text" autocomplete="off" value=""> </div> </div> </div> </div> </form> </div>
错误原因排查
- 未定义变量
$thisDate:控制器直接使用$thisDate但未赋值,触发Undefined variable错误。 - 空对象调用风险:
Attendance::find($req->input('ap_id'))可能返回null,后续调用$row->emp_id会触发Trying to get property of non-object错误;同理Employee::find($row->emp_id)返回null时,调用$rowEmp->emp_code也会报错。 - 字符串截取越界:如果
$req->input('ap_in')或$row->ap_in格式不符合预期(长度不足11位),substr返回的结果会异常,导致后续比较或写入数据库出错。 - 模型批量赋值未配置:
Attendance_raw_tmp模型未设置$fillable属性时,create方法会触发MassAssignmentException。 - 用户对象未初始化:
$this->_user->user_id如果_user未在控制器中赋值,会触发Undefined property错误。
修复方案
1. 补全变量定义与空值校验
修改控制器代码,添加变量赋值和空对象判断:
public function savepresence(Request $req) { // 校验考勤记录是否存在 $row = Attendance::find($req->input('ap_id')); if (!$row) { return redirect()->back()->with('notif', [ 'type' => 'error', 'message' => '考勤记录不存在' ]); } // 校验员工记录是否存在 $rowEmp = Employee::find($row->emp_id); if (!$rowEmp) { return redirect()->back()->with('notif', [ 'type' => 'error', 'message' => '员工记录不存在' ]); } // 定义日期,根据业务需求调整格式 $thisDate = date('Y-m-d'); // 处理上班时间逻辑 $apIn = $req->input('ap_in'); $ar_time_in = !empty($apIn) && strlen($apIn) >= 19 ? substr($apIn, 11, 8) : ''; $ar_time_in_old = strlen($row->ap_in) >= 19 ? substr($row->ap_in, 11, 8) : ''; if (!empty($apIn) && $ar_time_in != $ar_time_in_old){ Attendance_raw_tmp::create([ 'emp_id' => $row->emp_id, 'ar_date_tmp' => $thisDate, 'ar_id_tmp' => $rowEmp->emp_code, 'ar_time_tmp' => $ar_time_in, 'ar_lat' => '-6.3013773', 'ar_long' => '106.8429748', 'ar_client_tmp' => $req->input('ar_client_tmp'), 'ar_notes_tmp' => $req->input('ar_notes_tmp'), 'ar_opid' => auth()->id() // 改用Laravel默认auth获取登录用户ID ]); } // 处理下班时间逻辑 $apOut = $req->input('ap_out'); $ar_time_out = !empty($apOut) && strlen($apOut) >= 19 ? substr($apOut, 11, 8) : ''; $ar_time_out_old = strlen($row->ap_out) >= 19 ? substr($row->ap_out, 11, 8) : ''; if (!empty($apOut) && $ar_time_out != $ar_time_out_old){ Attendance_raw_tmp::create([ 'emp_id' => $row->emp_id, 'ar_date_tmp' => $thisDate, 'ar_id_tmp' => $rowEmp->emp_code, 'ar_time_tmp' => $ar_time_out_old, // 确认业务逻辑是否需要旧值 'ar_lat' => '-6.3013773', 'ar_long' => '106.8429748', 'ar_client_tmp' => $req->input('ar_client_tmp'), 'ar_notes_tmp' => $req->input('ar_notes_tmp'), 'ar_opid' => auth()->id() ]); } return redirect()->route('attendance.index')->with('notif', [ 'type' => 'success', 'message' => '考勤更新已保存' ]); }
2. 配置模型批量赋值
确保Attendance_raw_tmp模型设置可批量赋值字段:
class Attendance_raw_tmp extends Model { protected $fillable = [ 'emp_id', 'ar_date_tmp', 'ar_id_tmp', 'ar_time_tmp', 'ar_lat', 'ar_long', 'ar_client_tmp', 'ar_notes_tmp', 'ar_opid' ]; }
3. 查看错误日志定位问题
检查Laravel项目的storage/logs/laravel.log文件,里面会记录具体的错误堆栈信息,帮助精准定位问题。
4. 补充表单提交按钮
当前视图缺少提交按钮,添加到表单末尾:
<div class="form-group mb-4 row"> <div class="col-sm-9 offset-sm-3"> <button type="submit" class="btn btn-primary">保存修改</button> </div> </div>
内容的提问来源于stack exchange,提问作者Achmad7 Prasetyo
相关产品推荐
相关产品推荐

