2017-05-10 66 views
0

我有简单的添加,编辑和删除的方法,但随机我得到这个错误:得到TokenMismatchException在VerifyCsrfToken.php线68

TokenMismatchException在VerifyCsrfToken.php线68

有时没有任何错误,我可以添加,编辑或删除,有时我得到了错误,我不知道为什么。自从我在表格中使用csrf_field后,我得到了这个错误。

{{csrf_field()}} 

这是我的代码。 路线:

Route::resource('info','InfoController'); 

索引视图:

@if(Auth::check()) 
    <div class="info-btn"> 
     <a href="info">Add</a> 
     @if($info) 
      <a href="{{action('[email protected]',$info->id)}}">Edit </a> 

      {!! Form::open(['action' => 
     ['[email protected]',$info->id] , 'method' => 'DELETE']) !!} 
      {{Form::token()}} 
      {{form::submit('delete')}} 
      {{Form::close()}} 
     @endif 
    </div> 
@endif 

添加视图:

<form action="{{action('[email protected]')}}" method="post"> 
    {{csrf_field()}} 

    <div class="form-group"> 
     <label for="name">Name</label> 
     <input type="text" name="name" class="form-control" id="name"}"> 
    </div> 
     <label for="name">Slogan</label> 
     <input type="text" name="add-slogan" class="form-control" id="slogan"> 
    </div> 
    <div class="form-group"> 
     <label for="email">E-Mail:</label> 
     <input type="mail" name="email" class="form-control" id="email"> 
    </div> 
    <div class="form-group"> 
     <label for="phone">Phone :</label> 
     <input type="text" name="phone" class="form-control" id="phone"> 
    </div> 

    <button type="submit">Send Info</button> 
</form> 

编辑观点就像上面的表格,只有行动是不同

<form action="{{action('[email protected]',$edit_info->id)}}" method="post"> 
    {{csrf_field()}} 
    {{method_field('PUT')}} 

InfoController :

public function index() 
{ 
    $info=Info::all()->first(); 
    return view('info-add',compact('info')); 
} 

public function store(Request $request) 
{ 
    $name = $request->input('add-name'); 
    $slogan = $request->input('add-slogan'); 
    $email = $request->input('add-email'); 
    $phone = $request->input('add-phone'); 
    DB::table('data')->insert([ 
     'name' => $name, 
     'email' => $email, 
     'address' => $address, 
     'phone' => $phone, 
    ]); 
    return redirect('/'); 
} 

public function edit($id) 
{ 
    $edit_info=DB::table('data')->where('id',$id)->first(); 
    return view('info-edit',compact("edit_info")); 
} 

public function update(Request $request, $id) 
{ 
    DB::table('data')->where('id',$id)->update([ 
     'name' => $request->input('name'), 
     'phone' =>$request->input('phone'), 
     'email' =>$request->input('email'), 
    ]); 
    return redirect('/'); 
} 

public function destroy($id) 
{ 
    DB::table('data')->where('id',$id)->delete(); 
    return Redirect('/'); 
} 
+0

你可以检查并检查_token是否被传递? –

+0

我检查了我的表单,是的,我有_token“” – siros

+0

@siros,这个异常是在'csrf_token'不匹配时引起的。在你的控制器方法中,像'dd($ request)'一样转储'request'数组。并验证上面提到的注释中传递的'token'是否匹配'controller'方法中收到的内容。 –

回答

0

如果您确定_token被传递,您应该检查您正在使用的会话类型。默认情况下,laravel使用文件会话。我有这个问题使用沉重的ajax应用程序,并将会话驱动程序更改为数据库解决了我的问题。

了解更多关于此这里: https://laravel.com/docs/5.4/session#configuration

+0

我使用默认值,我的项目是非常简单的索引与CRUD和而已。我改变数据库的会话仍然有错误:( – siros

+0

你在什么平台?尝试检查文件夹的权限 chmod 777 storage/framework/sessions /(例如在linux中) –

+0

我使用Windows 10并且仍然是本地的,实际上我检查我的注册和登录表单,我得到了同样的错误。这很奇怪,因为昨天我没有任何错误 – siros

0

如果它发生的时间的时候,这可能是由于会话超时。如果您尝试刷新页面,可以检查它。 Check this laracast讨论。

相关问题