Rashed Hasan
Rashed Hasan

Reputation: 3751

update multiple columns value at once in laravel

I am trying to update attendance column outtime of all selected employees with checkout time.During intime check-in time successfully stored in the database, but I am getting trouble with the update. I did something like below, but it's updating the only first row. What should be written query would someone help me, please?

AttendanceController.php

public function checkout(Request $request)
{

    $request->validate([
        'outtime'  => 'required',
        'employee_id' => 'required',
    ]);


    $outTime    = Carbon::parse($request->outtime)->format('H:i:s');
    $date       = date('Y-m-d');
    $employeeIds = $request->employee_id;

    $data = [];


    foreach ($employeeIds as $employeeId) {
        $data[] = [
            'employee_id' => $employeeId,
            'attendance_date' => $date,
        ];

    }

    $update = Attendance::where('employee_id', $data)
                        ->where('attendance_date', $date)
                        ->update([
                            'outtime' => $outTime 
                        ]);


    return back()->with('success', 'Checked Out');       
}

Upvotes: 0

Views: 1103

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

Use this:

whereIn('employee_id', $employeeIds)

Upvotes: 1

Related Questions