0

I have simple Add, Edit and Delete methods but at random I get this error :

TokenMismatchException in VerifyCsrfToken.php line 68

Sometimes without any error I can add, edit or delete and sometimes I got the error and I don't know why. I got this error since I use csrf_field at my form.

{{csrf_field()}}

Here is my code. Route:

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

index view :

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

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

Add View:

<form action="{{action('InfoController@store')}}" 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>

Edit view is just like above form and only action is different

<form action="{{action('adminInfoController@update',$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('/');
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Siros Fakhri
  • 2,133
  • 5
  • 20
  • 31

2 Answers2

0

If you're sure that the _token is being passed, you should check what kind of Session you're using. By default laravel uses file sessions. I had that problem using heavy ajax applications, and changing the session driver to database solved my problem.

Read more about this here : https://laravel.com/docs/5.4/session#configuration

Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32
  • i use default value and my project is very simple index with CRUD and nothing more. i change session to database still got error :( – Siros Fakhri May 10 '17 at 09:45
  • What platform are you in? try to check permissions in folders chmod 777 storage/framework/sessions/ (in linux for example) – Sérgio Reis May 10 '17 at 09:52
  • i use windows 10 and still local and actually i check my register and login form and i got same error. it's weird because yesterday i have't any error – Siros Fakhri May 10 '17 at 10:04
  • I had a similar problem using xampp and the file session... Xampp wouldn't keep the session file open and the token would regenerate causing loss in session and same error on ajax requests... My fix was changing to file sessions http://stackoverflow.com/questions/37439721/unable-to-store-session-in-database-in-laravel-5-2 – Sérgio Reis May 10 '17 at 11:28
  • 1
    i just fresh install laravel and for now it's work fine. tnx for all answers :) – Siros Fakhri May 10 '17 at 12:23
0

If it happens time to time, this can be due to session time outs. You can check it if you try to refresh the page. Check this laracast discussion as well.

Achala Dissanayake
  • 810
  • 3
  • 16
  • 33