Giorgiovski
2 months ago
204
4
Laravel

Update just only one record by Eloquent - error

Posted 2 months ago by Giorgiovski

I want to update only the requested element but the query update all the entries from this table. I don't know why.

public function updateEverySessionByClick(Request $request, $trainerId){
        
        //split the string from one variable to two for hour and status like: $var = hour , $var2 = status because I am supposed to search in two columns, not in one. I have 09:00 Busy in one string
        $splitHourAndStatus = explode(" ", $request->sessionHourForUpdate);
        $searchSessionStatus = Session::where('added_by', $trainerId)
                                                           ->where('session_date', $request->sessionDateForUpdate)
                                                           ->where('session_hour', rtrim($splitHourAndStatus[0])) 
                                                           ->where('session_status', ltrim($splitHourAndStatus[1]))
                                                           ->first();

        $getOnlySessionStatus = $searchSessionStatus->pluck('session_status');
        $getOnlySessionStatus = substr($getOnlySessionStatus, 2, -2); //remove first 2 and last 2 chars from array. Like ["  "] to get just the status
        
        // Update the recent status with new one in the database
//############################################I THINK HERE IS THE PROBLEM
        // if($getOnlySessionStatus == 'Busy'){
            $updateRecentStatus = $searchSessionStatus;
            $updateRecentStatus->session_status = 'Open';
            $updateRecentStatus->save();
        // }

        //return success message
        return redirect('profile/'.auth()->user()->id)->with('success', $getOnlySessionStatus);
    }

After the function ends all my records are updated with "OPEN" status. All of them. Thank you in advance.

Please sign in or create an account to participate in this conversation.