csrf token error in the Laravel

Asked

Viewed 551 times

0

I’m getting the bug

Tokenmismatchexception in Verifycsrftoken.php (line 68)

when I submit the following view information. Detail is that in all my views I have csrf_token because of the Blade template. Anyway, this is my view:

@extends('layouts.app')

@section('content')
<div class="container">

    <h1>Funções do <b>{{$user->name}}</b></h1><br>

    <form action="{{url("/users/{$user->id}/roles/salva")}}" method="POST">
        <div class="form-group">
           <label for="exampleFormControlSelect1" class="col-md-4 control-label">Access Level</label>
           <select class="form-control" id="exampleFormControlSelect1">
               <option name="role_id" value="6">Viewer</option>
               <option name="role_id" value="5">Manager</option>
               <option name="role_id" value="3">Admin</option>
           </select>    
        </div>
        <button type="submit" class="btn btn-success">Adicionar Função</button>
    </form>
    <br>
    <form action="/users" method="get">   
        <button class="btn btn-danger">Voltar</button>
    </form>
</div>
@endsection

I don’t know yet if this dropdown will work but it doesn’t matter for now (I think)

@csrf in text form in view

...

1 answer

1


You should put inside your form @csrf.

@extends('layouts.app')

@section('content')
<div class="container">

    <h1>Funções do <b>{{$user->name}}</b></h1><br>

    <form action="{{url("/users/{$user->id}/roles/salva")}}" method="POST">
         @csrf
        <div class="form-group">
           <label for="exampleFormControlSelect1" class="col-md-4 control-label">Access Level</label>
           <select class="form-control" id="exampleFormControlSelect1">
               <option name="role_id" value="6">Viewer</option>
               <option name="role_id" value="5">Manager</option>
               <option name="role_id" value="3">Admin</option>
           </select>    
        </div>
        <button type="submit" class="btn btn-success">Adicionar Função</button>
    </form>
    <br>
    <form action="/users" method="get">   
        <button class="btn btn-danger">Voltar</button>
    </form>
</div>
@endsection

Another way of declaring csrf is:

{{ csrf_field() }}

Source: CSRF Protection

  • I had done this test, but when I opened the view it ran in text form. Like, it appeared written on the @csrf screen. But I’ll try again

  • There is another way to declare csrf, I edited the question and put

  • OK, thanks I’ll try

  • 1

    It worked, thanks

  • Please mark the question as answered by pressing the green "v".

  • have to wait 10min

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.