In the last lessons, we learned how to show pages using Routes → Controllers → Views.
Now let’s make our pages interactive by handling user input from forms.
To do that, we need to understand Requests and Validation.
What is a Request in Laravel?
Whenever a user visits a page or submits a form, Laravel creates a Request object containing all the data about that request — like form fields, query parameters, and files.
Creating a Simple Form
Let’s make a form to collect a user’s name and email.
In resources/views/contact.blade.php:
@extends('layouts.main')
@section('title', 'Contact Us')
@section('content')
<h2>Contact Form</h2>
<form action="{{ route('contact.submit') }}" method="POST">
@csrf
<label>Name:</label>
<input type="text" name="name">
<label>Email:</label>
<input type="email" name="email">
<button type="submit">Send</button>
</form>
@endsection
Adding a Route
In routes/web.php:
use App\Http\Controllers\ContactController;
Route::get('/contact', [ContactController::class, 'showForm'])->name('contact.show');
Route::post('/contact', [ContactController::class, 'submitForm'])->name('contact.submit');
Creating the Controller
Run:
php artisan make:controller ContactController
In app/Http/Controllers/ContactController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function showForm()
{
return view('contact');
}
public function submitForm(Request $request)
{
// Validation will go here
}
}
Validating User Input
Laravel’s $request->validate() method makes validation super simple.
Example:
public function submitForm(Request $request)
{
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email'
]);
return "Form submitted successfully!";
}
Showing Validation Errors in the View
Blade provides $errors to display validation messages.
In contact.blade.php, above the form:
@if($errors->any())
<div style="color: red;">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Preserving Old Input
If validation fails, Laravel redirects back with the old input.
You can keep previous values like this:
<input type="text" name="name" value="{{ old('name') }}">
<input type="email" name="email" value="{{ old('email') }}">
Common Validation Rules
| Rule | Description |
|---|---|
| required | Field must not be empty |
| Must be a valid email address | |
| min:x | Minimum length of x |
| max:x | Maximum length of x |
| numeric | Must be a number |
| unique:table | Value must be unique in a database |
Conclusion:
We can now handle forms, validate user input, and show error messages — an essential step toward building real applications.

