I am trying to store a historical data through API from front-end(flutter). So far both of the features(clock in and clock out) is working well and I am able to record and stored the data everytime data is sent from front-end(flutter). I separate the clock in and clock out into two different function in the controller and so is when I tested it in postman. The problem is that now, after I clock in, the data is stored in the database table and when I clock out, a new row is created as well on the table as shown here https://paste.pics/f756579f3256f511f828933cbfc52aac . I want it to be when I clock out, the 'time_checkOut', and 'location_checkOut' is stored within the same row and not a new row. At the same time, I want to be able to store a history record like daily record(yesterday, tomorrow, the day after tomorrow, everyday). How can I do this ? Below are the two functions in my controller:
clockIn controller
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 controller
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);
}
CodePudding user response:
To avoid creating a new row in the AttendanceRecord table when a user clocks out, you can use the updateOrCreate method instead of creating a new instance of AttendanceRecord and calling the save method on it. The updateOrCreate method will update an existing record if it exists, or create a new one if it doesn't. Here's an example of how you could use it:
// Retrieve the user's data
$users = User::where('staff_id', $r->staff_id)
->select(['staff_id', 'time_checkOut', 'location_checkOut'])
->first();
// Update the user's data with the current time and location
$mytime = Carbon::now();
$time = $mytime->format('H:i:s');
$date = $mytime->format('Y-m-d');
$users->time_checkOut = $time;
$users->location_checkOut = $r->location_checkOut;
// Save the updated data to the database
AttendanceRecord::updateOrCreate(
['staff_id' => $users->staff_id, 'date_checkIn' => $date],
$users->toArray()
);