首页 > 解决方案 > LARAVEL 8 - $REQUEST->NAME 返回 NULL

问题描述

美好的一天,我的代码有问题。我会更明确。

  1. 我有一个字段名称为“金额”的表格。当用户在输入金额后单击提交时,它必须向管理员发送电子邮件,并且当管理员单击通过电子邮件提供的链接时,该字段中的金额将被保存到数据库中。

专业问题是当我尝试使用 $request->amount 访问金额时。我得到“ null”。

将电子邮件发送给管理员以进行批准的控制器

    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");
    }

将数据保存到数据库的控制器 - [管理员在单击提供的链接后调用此控制器]

        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');   

到目前为止我做了什么。

自从我提交表单以来,我一直在怀疑 GET 方法,但不幸的是,当我将 GET 更改为 POST 时,我收到一条错误消息,指出不支持 GET...注意 [我从刀片文件和路由更改]。

主要问题:我无法访问$request->amount以保存它。

标签: phpgetlaravel-8

解决方案


route按照我在下面添加的方式添加金额,

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

那么无论你在哪里调用这个 URL,你都应该在令牌之后传递金额,检查下面的代码,并在你的public function sendApproveReplenishEmail(Request $request)

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

推荐阅读