2017-05-10 63 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討論。

相關問題