1
votes

I am using Laravel 5 and I'm getting an error when click submit on my form.

HTML

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <h3>This is a test page </h3>
    <form method="POST" action="./apartment/sayLove2">
        <input name="id" value="1" type="text"/>
        <input name="team" value="roma" type="text"/>
        <input name="bestPlayerInTheWorld" value="TOTTI" type="text"/>
        <input  value="Send" type="submit" />
    </form>
</body>
</html>

Route

Route::post('/apartment/sayLove2', 'ApartmentController@sayLove2');

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApartmentController extends Controller
{
    public function sayLove2(Request $request)
    {
        $id = $request->input('id');
        echo $id;
        exit;
        $team = $request->input('team');
        $bestPlayerInTheWorld = $request->input('bsetPlayerInTheWorld');

        return view('sayLove', ['id' => $id, $team => $team]);
    }
}

Error

TokenMismatchException in VerifyCsrfToken.php line 67: in VerifyCsrfToken.php line 67 at VerifyCsrfToken->handle(object(Request), object(Closure)) at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in StartSession.php line 62 at StartSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure)) at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Router.php line 726 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699 at Router->dispatchToRoute(object(Request)) in Router.php line 675 at Router->dispatch(object(Request)) in Kernel.php line 246 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Kernel.php line 132 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 at Kernel->handle(object(Request)) in index.php line 54 at require_once('/Applications/XAMPP/xamppfiles/htdocs/ULTest/public/index.php') in server.php line 21

4

4 Answers

3
votes

Take a look at here: https://laravel.com/docs/master/routing#csrf-protection

You need to add CSRF-TOKEN.

You can add a hidden input with your form like this:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
0
votes

try this if all configurations correct. i have also faced this problem and followed too many suggestions but finally, below are worked well to me.

php artisan cache:clear

php artisan config:clear

php artisan config:cache
0
votes

Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request.

You may use the @csrf Blade directive to generate the token field:

<form method="POST" action="/someurl">
    @csrf
    ...
</form>

You can learn more about it on laravel documentation site: https://laravel.com/docs/7.x/csrf#csrf-introduction

-1
votes

Shortest answer:

{!! csrf_field() !!}

See here.