I want to fix an error regarding an unassigned variable, but I don't know where to define it:

Use of unassigned variable '$userInfo'PHP(PHP1412)

The error is occuring in this line $accessToken = $userInfo->createToken(uniqid())->plainTextToken;.
This is my code:
public function createUser(Request $request)
{
    try {
        // Validate

        $validateUser = Validator::make($request->all(), [
            'avatar' => 'required',
            'type' => 'required',
            'open_id' => 'required',
            'name' => 'required',
            'email' => 'required|email|unique:users,email',
        ]);

        if ($validateUser->fails()) {
            return response()->json([
                'status' => false,
                'message' => 'validation error',
                'errors' => $validateUser->errors()
            ], 401);
        }

        // validated, will have all user field values
        // we can save in the database
        $validated = $validateUser->validated();

        $map = [];
        // email, phone, facebook, apple
        $map['type'] = $validated['type'];
        $map['open_id'] = $validated['open_id']; // if there is user in our database
        $user = User::where($map)->first();

        $user = User::where($map)->first();

        // whether user have already logged in or not
        // empty means does not exist
        // this token is user id
        if (empty($user->id)) {
            // this certain user has never been in our website
            // our job is to assign the user to database
            // this token is user id
            $validated["token"] = md5(uniqid().rand(10000,99999));
            // user first time created
            $validated['created_at'] = Carbon::now();
            // returns the id of row after saving
            $userID = User::insertGetId($validated); // we return the userid
            // users all info will be here
            $userInfo = User::where("id", "=", $userID)->first();

            $accessToken = $userInfo->createToken(uniqid())->plainTextToken;

            $userInfo->access_token = $accessToken;

            return response()->json([
                'status' => true,
                'message' => 'User Created Successfully',
                'data' => $userInfo
            ], 200);
        }

        $accessToken = $userInfo->createToken(uniqid())->plainTextToken;
        $userInfo->access_token = $accessToken;
        return response()->json([
            'status' => true,
            'message' => 'User Logged In Successfully',
            'token' => $userInfo
        ], 200);
    } catch (\Throwable $th) {
        return response()->json([
            'status' => false,
            'message' => $th->getMessage()
        ], 500);
    }
}


Laravel issue creating new user
The variable $userInfo is not defined before the line where the error is occurring. To fix this issue, you need to define $userInfo before using it. Replace the variable $userInfo with $user in the last part of the code.
public function createUser(Request $request)
{
    try {
        // Validated
        $validateUser = Validator::make($request->all(), [
            'avatar' => 'required',
            'type' => 'required',
            'open_id' => 'required',
            'name' => 'required',
            'email' => 'required|email|unique:users,email',
        ]);

        if ($validateUser->fails()) {
            return response()->json([
                'status' => false,
                'message' => 'validation error',
                'errors' => $validateUser->errors()
            ], 401);
        }

        // Validated, will have all user field values
        // We can save in the database
        $validated = $validateUser->validated();

        $map = [];
        $map['type'] = $validated['type'];
        $map['open_id'] = $validated['open_id'];
        $user = User::where($map)->first();

        // Whether user has already logged in or not
        // Empty means does not exist
        // This token is user id
        if (empty($user->id)) {
            // This certain user has never been on our website
            // Our job is to assign the user to the database
            // This token is user id
            $validated["token"] = md5(uniqid() . rand(10000, 99999));
            // User first time created
            $validated['created_at'] = Carbon::now();
            // Returns the id of row after saving
            $userID = User::insertGetId($validated);
            // Users all info will be here
            $userInfo = User::where("id", "=", $userID)->first();

            $accessToken = $userInfo->createToken(uniqid())->plainTextToken;
            $userInfo->access_token = $accessToken;

            return response()->json([
                'status' => true,
                'message' => 'User Created Successfully',
                'data' => $userInfo
            ], 200);
        }

        $accessToken = $user->createToken(uniqid())->plainTextToken;
        $user->access_token = $accessToken;

        return response()->json([
            'status' => true,
            'message' => 'User Logged In Successfully',
            'token' => $user
        ], 200);

    } catch (\Throwable $th) {
        return response()->json([
            'status' => false,
            'message' => $th->getMessage()
        ], 500);
    }
}

