skip to Main Content

In laravel controller function the coding has make changes and save it,when i refresh the webpage the coding is not reflected?

Actually code:-

public function generate(Request $request) 
    { # Validate Data $request->validate
    ([ 'reset_mailid' => 'required|exists:vs360_tables,mailid' ]);
    # Generate An OTP
        // Create a new instance of the Request class
        $request = $request->input('reset_mailid');
        
        // Call the generateOtp() method with the correct argument type
    
        $verificationCode = $this->generateOtp($request);
    
        $message = "Your OTP To Login is :- ".$verificationCode->otp;
        # Return With OTP 
    
         return redirect()->route('otp.verification', ['reset_mailid' => $verificationCode->reset_mailid])->with('success',  $message); 
    }
    
    public function generateOtp(Request $request)
    {
        $user = User::where('mailid', $request)->first();
    
        # User Does not Have Any Existing OTP
        $verificationCode = VerificationCode::where('reset_mailid', $user->reset_mailid)->latest()->first();
    
        $now = Carbon::now();
    
        if($verificationCode && $now->isBefore($verificationCode->expire_at)){
            return $verificationCode;
        }
    
        // Create a New OTP
        return VerificationCode::create([
            'reset_mailid' => $request->input('reset_mailid'),
            'otp' => rand(123456, 999999),
            'expire_at' => Carbon::now()->addMinutes(10)
        ]);
    }

AppHttpControllersAuthOtpController::generateOtp(): Argument #1 ($request) must be of type IlluminateHttpRequest, string given, called in D:localserverhtdocsLaravel_Projectsfinal_vserve360appHttpControllersAuthOtpController.php on line 32

2

Answers


  1. Chosen as BEST ANSWER

    i change the code in controller function as you say but the coding is not reflected in webpage when i refresh?

    {
    
        # Validate Data
    
        $request->validate([
    
            'reset_mailid' => 'required|exists:vs360_tables,mailid'
    
        ]);
    
    
    
        # Generate An OTP
    
        $verificationCode = $this->generateOtp($request->reset_mailid);
    
    
    
        $message = "Your OTP To Login is - ".$verificationCode->otp;
    
        # Return With OTP
    
    
    
        return redirect()->route('otp.verification', ['reset_mailid' => $verificationCode->reset_mailid])->with('success',  $message);
    
    }
    
    
    
    public function generateOtp(Request $request)
    
    {
    
        $reset_mailid = $request->reset_mailid;
    
        $user = User::where('mailid', $reset_mailid)->first();
    
        // dd('$reset_mailid');
    
        # User Does not Have Any Existing OTP
    
        // $verificationCode = VerificationCode::where('reset_mailid', $user->reset_mailid)->latest()->first();
    
    
    
        // $now = Carbon::now();
    
    
    
        // if($verificationCode && $now->isBefore($verificationCode->expire_at)){
    
        //     return $verificationCode;
    

  2. You have a problem in your generate method, what your are doing actually is replacing the laravel request object inot reset_mailid, so the request variable has a string now, change your code like this:

    $request_mail_id = $request->input('reset_mailid');
    // in the avobe line you have to change the variable name into something 
    // else rather then request 
    
    // Call the generateOtp() method with the correct argument type
    // now pass the new variable into your function
    
    $verificationCode = $this->generateOtp($request_mail_id);
    

    In the second function you dont need http request, just receive the parameter in any name, change the code like below:

    public function generateOtp($mail_id)
    {
        $user = User::where('mailid', $mail_id)->first();
    
        # User Does not Have Any Existing OTP
        $verificationCode = VerificationCode::where('reset_mailid', $user- 
        >reset_mailid)->latest()->first();
    
        $now = Carbon::now();
    
        if($verificationCode && $now->isBefore($verificationCode->expire_at)){
            return $verificationCode;
        } 
    
        // Create a New OTP
        return VerificationCode::create([
            'reset_mailid' => $request->input('reset_mailid'),
            'otp' => rand(123456, 999999),
            'expire_at' => Carbon::now()->addMinutes(10)
        ]);
    }
    

    IT should work now

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search