Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

TokenMismatchException in VerifyCsrfToken.php line 68:

controllers/MenuController.php

  <?php

  namespace App\Http\Controllers;

   use App\User;
   use App\Http\Controllers\Controller;

   use Illuminate\Foundation\Bus\DispatchesJobs;
   use Illuminate\Routing\Controller as BaseController;
   use Illuminate\Foundation\Validation\ValidatesRequests;
   use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

   use Illuminate\Http\Request;

   class MenuController extends Controller
  {
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

  public function getMenu()
  {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_id' => '1', 'menu_color' => $menu_color]);

   return view();
   }
 }

routes/web.php

 Route::get('/', function () {
     return view('welcome');
 });

  Route::post('route/to/thing',   'MenuController@getMenu')->name('menu.color');

views/welcome.blade.php

    <div class="content">
    
        <div class="content">

        <div class="title m-b-md">
            MENU CSS
        </div>

        Pick a new color for menu css:<br><br>
        <form action="{{route('menu.color')}}" method="post">
            <select name="cars">
                <option value="red">Red</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="orange">Orange</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>                  
        </div>
                        
    </div>    
        

The error is: TokenMismatchException in VerifyCsrfToken.php line 68:

I don't think I place any: csrf_token()

0 likes
14 replies
davy_yg's avatar
Level 27

I read the documentation but cannot quite understand it. Why would I need csrf to fix the error. Even so I try to add csrf_field(). Like this:

views/welcome.blade.php

    Pick a new color for menu css:<br><br>
        <form action="{{route('menu.color')}}" method="post">
             {{ csrf_field() }}
        
            <select name="cars">
                <option value="red">Red</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="orange">Orange</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>                  
        </div>

The error still remains.

bunnypro's avatar

did you read the documentation ? it is explained there.

davy_yg's avatar
Level 27

First, how laravel automatically generates a CSRF "token" for each active user session?

Second I try to store token in a HTML meta tag:

by adding:

   <meta name="csrf-token" content="{{ csrf_token() }}">

Yet the error still appears.

davy_yg's avatar
Level 27

I did finished that up once. Which episodes should I watch to get the specific error free?

davy_yg's avatar
Level 27

I watches the video but still do not understand what I did wrong, I try do this :

views/welcome.blade.php

    <div class="content">
    
        <div class="content">

        <div class="title m-b-md">
            MENU CSS
        </div>

        Pick a new color for menu css:<br><br>
        <!--  {{route('menu.color')}} -->
        <!-- <form action="{{route('menu.color')}}" method="post"> -->
        <form method="POST" action="/posts">
            {{ csrf_field() }}  
            <select name="cars">
                <option value="red">Red</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="orange">Orange</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>                  
        </div>
                        
    </div>    

I do not understand how to imitate the video.

Object not found!

Snapey's avatar

you say you know php

look in your controller. The first thing you do is try to use $request variable .... do you see anywhere in that function where $request gets initialised?

davy_yg's avatar
Level 27

You mean like this:

 use Illuminate\Http\Request;  // innitialize

 public function getMenu()
 {
 $menu_color = $request->get('cars');  // using $request

I also wonder by the way: how many lesson tutorial are there exist in laravel forum? Lol. Since I see that you have taken like 760 lessons?

Is there more than that? lol.

Snapey's avatar

no, you still don't have $request

yes, there are more lessons than that

davy_yg's avatar
Level 27

If I don't have $request then what should I do?

I think I copy and paste someone else codes that helped me - I forget who.

MuhammadShuja's avatar

do it this way:

public function getMenu(Request $request)
 {
 $menu_color = $request->get('cars');
...

or use the helper function like this:

public function getMenu()
 {
 $menu_color = request()->get('cars');
...

Please or to participate in this conversation.