这是联系表格。我想接收电子邮件并将此数据保存到我的mysql。我用Laravel。电子邮件功能运作良好。但有一个问题。我想将所有数据存储在“功能完成”处。 我在“功能确认”处验证所有数据。这是确认屏幕页面,因此用户仍未提交。我试图在“函数完成”处编写这样的代码,但错误提示“未定义变量:请求”,请教我如何修复代码?
public function confirm(Request $request)
{
$rules = [
'title' => 'required',
'search' => 'required',
'amount' => 'required|integer',
'email' => 'required|email',
'body' => 'required',
];
$this->validate($request, $rules);
$data = $request->all();
$request->session()->put($data);
return view('mail.confirm', compact("data"));
}
public function complete()
{
$data = $request->all(); # 3)
$request->session()->put($data); # 4)
Contact::create($request->all());
$data = session()->all();
Mail::send([ ・・・
答案 0 :(得分:0)
您必须像在Confirm函数中那样传递参数类型$ request。在您的完整函数中,您无需声明$ request变量并且无需声明即可访问它
public function confirm(Request $request)
{
$rules = [
'title' => 'required',
'search' => 'required',
'amount' => 'required|integer',
'email' => 'required|email',
'body' => 'required',
];
$this->validate($request, $rules);
$data = $request->all();
// setting session key value for you data
$request->session()->put('data',$data);
return view('mail.confirm', compact("data"));
}
/*
* complete page
*/
public function complete(Request $request)
{
// after confirm button click get data from session with key '@data' ;
$data = $request->session()->pull('data');
// get token value in variable and remove from data set so we can use mass assignement
$token = array_shift($data);
// creating record
$Contact = Contact::create($data);
Mail::send(['text' => 'mail.temp'], $data, function($message) use($data){
$message->to($data["email"])->bcc('lara_admin@sakura.ne.jp')->from('1110.ne.jp')->subject('thnak you。');});
Mail::send(['text' => 'mail.admintemp'], $data, function($message) use($data){
$message->to('lara_admin@sakura.ne.jp')->from('emailconf@.ne.jp')->subject('you got order');});
$data = session()->regenerateToken();
return view('mail.complete');
}