我尝试通过前端的API存储历史数据(扑动)。到目前为止这两个特性(* 打卡上班 * 和 * 打卡下班 *)运行良好,每次从前端发送数据时,我都能够记录和存储数据我在控制器中将 clock in 和 clock out 分成两个不同的功能,在我 * 打卡上班 * 之后,数据被存储在数据库表中,而当我 * 打卡下班 * 时,也会在表中创建一个新行,如此处所示https://paste.pics/f756579f3256f511f828933cbfc52aac。我希望在我 * 打卡下班 * 时,“time_checkOut”和“location_checkOut”被存储在同一行中,而不是一个新行中。同时,我希望能够存储历史记录,如每日记录(昨天,明天,后天,每天)。我如何做到这一点?下面是我的控制器中的两个功能:
clockIn控制器
public function userClockIn(Request $r)
{
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkIn', 'location_checkIn'])->first();
$mytime = Carbon::now();
$time = $mytime->format('H:i:s');
$date = $mytime->format('Y-m-d');
$users->date_checkIn = $date;
$users->time_checkIn = $time;
$users->location_checkIn = $r->location_checkIn;
$users->save();
// Retrieve current data
$currentData = $users->toArray();
// Store current data into attendance record table
$attendanceRecord = new AttendanceRecord();
$attendanceRecord->fill($currentData);
$attendanceRecord->save();
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
clockOut控制器
public function userClockOut(Request $r)
{
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'time_checkOut', 'location_checkOut'])->first();
$mytime = Carbon::now();
$time = $mytime->format('H:i:s');
$users->time_checkOut = $time;
$users->location_checkOut = $r->location_checkOut;
$users->save();
// Retrieve current data
$currentData = $users->toArray();
// Store current data into attendance record table
$attendanceRecord = new AttendanceRecord();
$attendanceRecord->fill($currentData);
$attendanceRecord->save();
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
1条答案
按热度按时间xytpbqjk1#
若要避免在用户打卡下班时在AttendanceRecord表中创建新行,您可以使用updateOrCreate方法,而不是创建AttendanceRecord的新示例并对它调用保存方法。updateOrCreate方法将更新现有记录(如果存在),或创建新记录(如果不存在)。下面是一个如何使用该方法的示例: