简体   繁体   中英

LARAVEL 8 - $REQUEST->NAME RETURNS NULL

Good day, I have an issue with my codes here. I'll be more explicit.

  1. I have a form with the field name "Amount". when the user clicks on submit after entering the amount it has to send an email to the admin, and when the Admin clicks on the link provided via Email, the amount from the field will be saved to the database.

The pro problem is when I try to access the amount with $request->amount. I m getting "null".

CONTROLLER THAT SENDS THE EMAIL TO THE ADMIN TO APPROVE

    public function sendApproveReplenishEmail(Request $request) {
   
        //$IP_PORT =$_SERVER['REMOTE_ADDR'].":".$_SERVER['REMOTE_PORT'];
        $IP =$_SERVER['REMOTE_ADDR'];

        //pass the userId of the person you want to activate
        // $user_id =Auth::user()->id;

        //Create id
        $id = $request->id;

        //Create token
        $token = (string) Str::uuid();

        //save token in DB
        $result = User::where('id', $id)
              ->update(['notify_token' => $token]);

        if($result != 1){ //let us return if we cannot update
            return nl2br ("\n We could not save token");
        }

        $activationUrl = 'http://'.$IP.':8001/admin/approve-replenish/'.$token;
        
        $details =[
            'title' => 'Please approve replenish by clicking the link below:',
            'body' =>  $activationUrl
        ];
 
        Mail::to('app77.test@gmail.com')->send(new ApproveReplenishMail($details));

        return nl2br ("\nReplenish approval Email sent");
    }

THE CONTROLLER THAT SAVES THE DATA TO THE DATABASE - [the admin calls this controller after clicking the provided link]

        try{
           
            $tokenid =$request->token;
         
            $user = User::select('name','id')->where('notify_token', $tokenid)->first();
            

            //model User should not be empty
           if(is_null($user)) {
                return nl2br ("\nUser with token .$tokenid. not found");
            }

         
            // $userId =$user->id;
            // $userId = User::select('id')->where('notify_token', $tokenid)->first();
            // echo nl2br ("\n$userId");

            $userId = $user->id;
           
           $float = Balance::where('user_id',$userId)->first();
            
           //model Balance should not be empty
           if(is_null($float)) {
               return nl2br ("Record with id .$userId. not found");
           }else{
               if($float->float > 0){
                   $float->float = $float->float + $request->amount;
               }else{
                   $float->float=$request->amount;
               }
               $float->user_id=$userId;
               $float->save();

               $request->session()->flash('success', 'You have successfully updated the client float');
      
               //we can delete token if necessary
               $user->update(['notify_token' => null]);
               }
      }catch (Exception $e) {
           echo $e;
       }
       return redirect(url('admin/float/floats'));
   }```

WEB.PHP ROUTES FOR THOSE CONTROLLERS

```Route::get('/approve-replenish/{token}', [ReplenishController::class,'approveReplenish'])->name('approve-replenish');
    Route::get('/send-replenish-mail/{id}', [ReplenishController::class,'sendApproveReplenishEmail'])->name('send-replenish-mail');   

WHAT I HAVE DONE SO FAR.

I was suspecting the GET Method since I m submitting the form, but unfortunately when I change GET to POST I get an error message saying GET is not supported... NB[I change from the blade file and routes].

MAIN ISSUE : I m not able to access $request->amount in order to save it.

Add amount to your route just the way I added in this one below,

Route::get('/approve-replenish/{token}/{amount}

then wherever you are calling this URL you're supposed to pass the amount right after the token, check the code below, and modify it accordingly in your public function sendApproveReplenishEmail(Request $request)

$activationUrl = 'http://'.$IP.':8001/admin/approve-replenish/'.$token.'/'.$youramount;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM