php - LARAVEL 8 - $REQUEST->NAME 返回 NULL
问题描述
美好的一天,我的代码有问题。我会更明确。
- 我有一个字段名称为“金额”的表格。当用户在输入金额后单击提交时,它必须向管理员发送电子邮件,并且当管理员单击通过电子邮件提供的链接时,该字段中的金额将被保存到数据库中。
专业问题是当我尝试使用 $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
以保存它。
解决方案
route
按照我在下面添加的方式添加金额,
Route::get('/approve-replenish/{token}/{amount}
那么无论你在哪里调用这个 URL,你都应该在令牌之后传递金额,检查下面的代码,并在你的public function sendApproveReplenishEmail(Request $request)
$activationUrl = 'http://'.$IP.':8001/admin/approve-replenish/'.$token.'/'.$youramount;
推荐阅读
- python - 不返回对 YAML 文件的更新并在字符串更新时返回 TypeError
- c# - 没有文字转语音的 Nexmo 语音通话
- bazel - 从源文件路径派生 Bazel 标签
- python - 正则表达式在子字符串之前捕获所有内容
- python - Python Messenger Bot 仅适用于管理员,不适用于所有人
- git - Teamcity 仅从一个分支触发标签
- wordpress - Wordpress Multisite - API 帖子返回一个空数组
- rendering - HoloLens 外部渲染
- javascript - 在 React JS 中生成随机数
- .net - xUnit 单元测试项目未读取 App.config