Laravel 正在访问其他函数的变量

Laravel Accessing variable of other function

我有一个 sendOtp() 方法,我必须使用该方法将 otp 发送给用户以在 changePassword() 方法

中更改密码

我想 return $otpsendOtp()changePassword()

sendOtp方法

public function sendOtp($user_phone_number)
{
    $otp = mt_rand(100000, 999999); 

    //sms login

    $data = [
        'data' => [
            'otp' => $otp
        ]
    ];

    return Response::json(
        $data_with_status = array_merge($this->respondSuccess('otp sent'), $data)
    );
}

changePassword()方法

public function changePasswordOtp($user_phone_number)
{
    $user = User::where('phone_number', '=', $user_phone_number)->count();

    if($user > 0) {

        $this->sendOtp($user_phone_number); 

        //return response with sent otp
    }
    else {

        return Response::json(
            $this->respondFailure('User not found')
        );

    }
}

谢谢

像这样更改您的 sendOtp :

public function sendOtp($user_phone_number)
{
    $otp = mt_rand(100000, 999999); 

    //sms login

    $data = [
        'data' => [
            'otp' => $otp
        ]
    ];
    $responseMessage= $this->respondSuccess();
    $responseArray = array_merge(responseMessage, data);
    return Response::json($responseArray); 
    );
}

您可以通过以下代码接收otp

public function changePasswordOtp($user_phone_number)
{
    $user = User::where('phone_number', '=', $user_phone_number)->count();

    if($user > 0) {

        $result_otp = $this->sendOtp($user_phone_number); 
         $result_otp = json_decode($result_otp ,true);

        $otp = $result_otp['data']['otp']; //return response with sent otp
    }
    else {

        return Response::json(
            $this->respondFailure('User not found')
        );

    }
}