This article is not maintained any longer and likely not up to date. DuckDuckGo might help you find an alternative.

Laravel Forms 101

I need to admit, I always have to look up how forms work in Laravel. So consider this blog post a quick overview – and a cheatsheet for myself in several months.

Here is how a simple form with one input field, say a fullname, and server-side validation looks like. I will annotate every line. I use TailwindCSS together with the form plugin to get a decent UI with minimal extra markup.

<!-- Blade, e.g. welcome.blade.php -->
<form action="{{ route('form.submit') }}" method="POST" class="space-y-4">
        @csrf

        <div>
            @error('fullname')
            <div class="text-red-500">{{ $message }}</div>
            @enderror

            <label for="fullname">Fullname</label>
            <input type="text" name="fullname" value="{{ old('fullname') ?? '' }}" class="form-input">

        </div>

        <div>
        
            <button type="submit">Submit</button>

        </div>
    
    </form>

To get your form working, you need to catch the submission. Here's the code for that with some basic validation:

// routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;


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

Route::post('/submit', function (Request $request) {
    $request->validate([
        'fullname' => 'required|alpha|min:3',
    ]);

    return 'Submission allowed';
})->name('form.submit');

Did this help you?