'Laravel post contact form giving me error 419

I've been trying to get a contact form to display as /contact when using the form, and after submitting the form. I'm getting Error 419 no matter what I seem to try.

I created a resource controller called ContactController to get this to work, but if I use Route::resource I'd be stuck using /contact/create as the actual form.

Here are my contact Routes for now:

//Route::resource('contact', 'ContactController');

Route::post('/contact', 'ContactController@store');

Route::get('/contact', 'ContactController@create');

Here is my contact form:

<form class="form" method="POST">
    @error('name')
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" placeholder="Enter Your Name....">
    </div>
    @error('email')
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
    <div class="form-group">
        <label for="email">Email</label>
        <input type="text" id="email" name="email" class="form-control @error('email') is-invalid @enderror" placeholder="[email protected]">
    </div>
    @error('subject')
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
    <div class="form-group">
        <label for="subject">Subject</label>
        <input type="text" id="subject" name="subject" class="form-control @error('subject') is-invalid @enderror" placeholder="What is it you need?">
    </div>
    @error('content')
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
    <div class="form-group">
        <label for="content">Content</label>
        <textarea class="form-control  @error('content') is-invalid @enderror" name="content" id="content" rows="3" placeholder="Tell me more..."></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

I'm sure there's something I'm doing wrong, so what could it be?



Solution 1:[1]

To prevent cross-site request forgery attacks, Laravel requires CSRF "tokens" to be present in your forms. Change

<form class="form" method="POST">

to

<form class="form" method="POST">
    @csrf

to include a hidden CSRF token field in the form.

https://laravel.com/docs/7.x/csrf

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brombeer