'How to update the timeout field in the attendance table for a given user_id, logdate and status using if else statement?

I have been trying to update the timeout for a given user_id, logdate, and status but my update statement is not working or may be my other if else are not correct. The first thing that I do is to check for the user_id in the user table. If the user's id exist in the user table then I check if that user_id exist in the attendance table. If the user's id, logdate exist in the attendance table (i.e the user was already sign in) then I need to update his/her timeout record. As for now my timeclock function can save a time in only and if the user sign out saved as timein. Any help please

I have two tables

users(id,name,email)

attendances(user_id,timein (string),timeout (string),logdate (date))

My route

   Route::get('/attendance/timeclock/{id}', [AttendanceController::class,'timeclock'])->name('attendance.timeclock');

My methods

 public function timeclock(Request $request, $id){

     $date = date('Y-m-d');
     $time = date('H:i:s A');
    // SELECT * FROM users WHERE id = '$id'
    $user = DB::table("users")
         ->where("id", "=", $id)
         ->get();
         //if the user is not found 
         if($user === null){
         // then
        return response()->json([
            "error" => trans("You enter an invalid ID.")
        ]);
        
    }
    
    else{

    //if the user id is found then
    // SELECT * FROM attendances WHERE user_id ='$id' AND LOGDATE='$date' AND STATUS='0'
    // SELECT user_id, logdate, timein,timeout, status FROM users 
    // left join attendances on users.id = attendances.user_id where attendances.user_id = 1
    //  and attendances.logdate ='2022-05-12' and attendances.status = 0

    $attendance =   DB::table("users")
    ->leftJoin("attendances", function($join){
        $join->on("users.id", "=", "attendances.user_id");
    })
    ->select("user_id", "logdate", "timein", "timeout", "status")
    ->where("attendances.user_id", "=", $id)
    ->where("attendances.logdate", "=", $date)
    ->where("attendances.status", "=", 0)
    ->get();

     // dd($attendance);
    //check if there is an attendance record for  given date and user id
    if(!isset($attendance)){

    // if  a given user id exit in the attendances table then
    // UPDATE attendance SET TIMEOUT='$time', STATUS='1' WHERE user_id='$user_id' AND LOGDATE='$date'   
    DB::table('attendances')
        ->where(['user_id' => $id, 'logdate' => $date] )
        ->update([
            'timeout' => $time,
            'status'=> '1',
            
        ]);

        return response()->json([
            "success" => trans("You enter an valid bbID.")
        ]);
    }
    
    else{

    // if there is no record for a given user id then 
    // INSERT INTO attendance(user_id,TIMEIN,LOGDATE,STATUS) VALUES('$user_id','$time','$date','0')

    $result = new Attendance;
    $result->user_id = $id;
    $result->created_by = 3;
    $result->timein = $time;
    $result->logdate =  $date;
    $result->status = 0;
    $result->save();

    return response()->json([
        "success" => trans("You enter an valid ID.")
    ]);

    }

    }
        
        

return view('fms.attendances.set_attendance.blade',$data)->with('message', 'Inserted successfully.');
 }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source