VerifyController.php

verifyUser

public function verifyUser(Request $request)
{
    // Extract the verification code from the request
    $verificationCode = $request->input('verificationCode');
    // Search for a user with the provided verification code in the UserAccount model
    $user = UserAccount::where('verification_code', $verificationCode)->first();
    // Check if a user with the given verification code is found
    if (isset($user)) {
        // If found, set the 'verified' attribute to true and save the user
        $user->verified = true;
        $user->save();
        // Prepare a success response
        $response = [
            'SUCCESS' => 1,
        ];
        // Return the success response
        return $response;
    } else {
        // If no user is found with the given verification code, prepare a failure response
        $response = [
            'SUCCESS' => 0,
        ];
        // Return the failure response
        return $response;
    }
}

To explain what this verifyUser method does:

  1. Receive Request Parameter:

    • The function takes a Request object as a parameter, representing an HTTP request.

    • It extracts the 'verificationCode' from the request using $request->input('verificationCode').

  2. Search for User with Verification Code:

    • It then uses the Laravel Eloquent ORM to search for a user in the UserAccount model where the 'verification_code' matches the extracted verification code.

    • The result of the query is stored in the $user variable.

  3. Check if User Found:

    • It checks if a user with the given verification code is found by using isset($user).

  4. Update User Verification Status:

    • If a user is found, it sets the 'verified' attribute of the user to true using $user->verified = true.

    • It then saves the changes to the user model with $user->save().

  5. Prepare Success Response:

    • If a user is found and successfully verified, it prepares a success response array with 'SUCCESS' => 1.

  6. Prepare Failure Response:

    • If no user is found with the given verification code, it prepares a failure response array with 'SUCCESS' => 0.

  7. Return Response:

    • Finally, it returns the prepared response (either success or failure) as a JSON response to the client making the request.

Last updated