laravel 数据单独存储,而不是同时存储

xhv8bpkk  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(115)

我尝试通过前端的API存储历史数据(扑动)。到目前为止这两个特性(* 打卡上班 * 和 * 打卡下班 *)运行良好,每次从前端发送数据时,我都能够记录和存储数据我在控制器中将 clock inclock 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);
    }
xytpbqjk

xytpbqjk1#

若要避免在用户打卡下班时在AttendanceRecord表中创建新行,您可以使用updateOrCreate方法,而不是创建AttendanceRecord的新示例并对它调用保存方法。updateOrCreate方法将更新现有记录(如果存在),或创建新记录(如果不存在)。下面是一个如何使用该方法的示例:

// 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()
);

相关问题