我尝试通过帖子表单发送请求但是错误TokenMismatchException
这是我的控制器代码
public function postSaveedit(Request $request) {
$var1 = $request->input('title'); //name on the form
$var2 = $request->input('meaning'); //name on the form
$words = User::where("title", $var1)->update(array("meaning" => $var2));
return view('dict.watch', compact('words'));
}
这是视图代码。
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>
向你求助
答案 0 :(得分:2)
来自https://laravel.com/docs/master/routing:
Laravel为每个活跃用户自动生成CSRF“令牌” 由应用程序管理的会话。此令牌用于验证 经过身份验证的用户是实际向其发出请求的用户 应用
无论何时在应用程序中定义HTML表单,都应该包含 表单中隐藏的CSRF令牌字段,以便CSRF保护 中间件将能够验证请求。生成隐藏 输入字段_token包含CSRF令牌,您可以使用 csrf_field辅助函数
只需在表单中添加此行
即可<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>