In This Article
Almost every business website needs a contact form — and Laravel makes it one of the easiest features to build properly. In this tutorial you will build a complete contact form in Laravel 13: a Blade form, server-side validation, saving messages to the database, sending the message by email, spam protection, and a quick test to prove it all works.
By the end you will have a contact page you can ship today. You need a working Laravel 13 project, PHP 8.3 or newer, and Composer. Laravel 13 requires PHP 8.3 or above, and the current release at the time of writing is Laravel 13.45. If you are starting from scratch, create a project with laravel new contact-form-demo and follow along.
What You Will Build
The form has three fields — name, email and message — and the flow has five parts. The visitor fills in the form and submits it. Laravel validates the input on the server. The message is saved to a contact_messages table so nothing is lost even if email fails. A Mailable sends the message to your team inbox, with the visitor's address set as the reply-to so you can answer with one click. Finally, a honeypot field and a rate limit keep spam out. Each part is small, and each one uses plain Laravel features — no extra packages required.
Step 1: Create the Model, Migration and Controller
Start by generating the pieces you need. One Artisan command creates the model and its migration together, and another creates the controller and the form request class for validation:
php artisan make:model ContactMessage -m
php artisan make:controller ContactController
php artisan make:request StoreContactRequest
php artisan make:mail ContactMail
Open the migration that was created in database/migrations and define the table. Keep it simple — a name, an email, a message and an optional IP address for spam review:
Schema::create('contact_messages', function (Blueprint $table) {
$table->id();
$table->string('name', 80);
$table->string('email');
$table->text('message');
$table->string('ip_address', 45)->nullable();
$table->timestamps();
});
Then run the migration and open the ContactMessage model to allow mass assignment for the four fields:
php artisan migrate
// app/Models/ContactMessage.php
protected $fillable = ['name', 'email', 'message', 'ip_address'];
Step 2: Create the Contact Form Blade View
Create resources/views/contact.blade.php with a simple form. Note the three Laravel essentials: the @csrf directive, which protects the form from cross-site request forgery; the old() helper, which keeps the visitor's input if validation fails; and the @error directive, which shows the message next to the field that failed:
<form method="POST" action="{{ route('contact.store') }}">
@csrf
<label>Your name</label>
<input type="text" name="name" value="{{ old('name') }}" required>
@error('name') <p>{{ $message }}</p> @enderror
<label>Your email</label>
<input type="email" name="email" value="{{ old('email') }}" required>
@error('email') <p>{{ $message }}</p> @enderror
<label>Your message</label>
<textarea name="message" rows="5" required>{{ old('message') }}</textarea>
@error('message') <p>{{ $message }}</p> @enderror
<!-- Honeypot: real visitors never see or fill this field -->
<input type="text" name="website" style="display:none">
<button type="submit">Send message</button>
</form>
@if (session('status'))
<p>{{ session('status') }}</p>
@endif
The hidden website field is the honeypot. Real visitors never see it, so it stays empty. Spam bots fill in every field they find — including hidden ones — which is exactly how you catch them in the next steps.
Step 3: Add the Routes
Add two routes in routes/web.php: one to show the form, one to handle the submission. The submission route gets a throttle so one visitor cannot flood your inbox:
use App\Http\Controllers\ContactController;
Route::get('/contact', [ContactController::class, 'show'])->name('contact.show');
Route::post('/contact', [ContactController::class, 'store'])
->middleware('throttle:5,60')
->name('contact.store');
The throttle:5,60 middleware allows five submissions per hour from one IP address. Five is generous for a real visitor and far too few for a spam bot. If you expect a busy contact page, raise the number — but keep a limit. A form with no rate limit is an open invitation to abuse.
Step 4: Validate With a Form Request
Never trust data from a form. Laravel's Form Request class keeps your validation rules in one clean place, and Laravel runs them before your controller code even starts. Open app/Http/Requests/StoreContactRequest.php and add the rules:
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:80'],
'email' => ['required', 'email', 'max:255'],
'message' => ['required', 'string', 'min:20', 'max:5000'],
];
}
public function messages(): array
{
return [
'message.min' => 'Your message needs at least :min characters.',
];
}
The messages() method is optional, but it is worth using: friendly error messages tell the visitor what to fix instead of showing Laravel's default wording. If validation fails, Laravel automatically redirects the visitor back to the form with the errors and their old input — you do not need to write that logic yourself.
Step 5: Handle the Submission in the Controller
Now open ContactController. The store method type-hints the Form Request, so by the time the method runs, the data is already validated. Check the honeypot, save the message, send the email, and redirect with a success message:
use App\Http\Requests\StoreContactRequest;
use App\Mail\ContactMail;
use App\Models\ContactMessage;
use Illuminate\Support\Facades\Mail;
public function show()
{
return view('contact');
}
public function store(StoreContactRequest $request)
{
// Honeypot: a bot filled the hidden field — drop the message quietly.
if ($request->filled('website')) {
return back()->with('status', 'Thanks, we received your message.');
}
$data = $request->validated();
$message = ContactMessage::create([
...$data,
'ip_address' => $request->ip(),
]);
Mail::to(config('mail.contact_address'))
->send(new ContactMail($message));
return back()->with('status', 'Thanks, we received your message.');
}
Notice two details. First, the honeypot check returns the same success message as a real submission — never tell a bot it was caught, or it will simply try a different trick. Second, the message is saved to the database before the email is sent, so even if your mail server fails, the enquiry is not lost.
Step 6: Send the Email With a Mailable
Open app/Mail/ContactMail.php and set the subject and the reply-to address. The reply-to is the detail most tutorials skip, and it is the one your team will notice: pressing Reply in the inbox answers the visitor directly instead of replying to your own no-reply address.
use App\Models\ContactMessage;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
class ContactMail extends Mailable
{
public function __construct(public ContactMessage $contactMessage) {}
public function envelope(): object
{
return new Envelope(
subject: 'New contact message from ' . $this->contactMessage->name,
replyTo: [new Address($this->contactMessage->email, $this->contactMessage->name)],
);
}
public function content(): object
{
return new Content(
view: 'emails.contact',
);
}
}
Create the email view at resources/views/emails/contact.blade.php with the message details, then set your mail settings in .env. While you are testing, use the log mailer so nothing leaves your machine:
MAIL_MAILER=log
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="Your Website"
With the log mailer, every email is written to storage/logs/laravel.log instead of being sent. When you are ready for real mail, change MAIL_MAILER to smtp and add your provider's host, port, username and password. For a busy site, make the Mailable implement ShouldQueue so the visitor never waits for your mail server — the controller responds instantly and a queue worker sends the email in the background.
Step 7: Test the Form
Test three things before you ship. First, submit a valid message and check that it appears in the contact_messages table and in the log file. Second, submit the form with an empty message and an invalid email — you should be redirected back with errors and your input still filled in. Third, submit the form with the hidden website field filled (you can do this from your browser's developer tools) and confirm the message is dropped while the page still shows the success text.
For automated testing, Laravel's HTTP tests make this easy. A single test can post to the route, assert the redirect, and assert the database has the message:
public function test_contact_form_saves_a_valid_message(): void
{
$response = $this->post(route('contact.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'message' => 'This is a test message with enough text.',
]);
$response->assertRedirect();
$this->assertDatabaseHas('contact_messages', [
'email' => 'test@example.com',
]);
}
Common Mistakes to Avoid
| Mistake | What happens | Fix |
|---|---|---|
| Validating only in JavaScript | Bots skip the browser and post directly | Always validate on the server with a Form Request |
| No rate limit on the route | One bot can flood your inbox in minutes | Add the throttle middleware to the POST route |
| No reply-to address | Your team replies to a no-reply address | Set replyTo to the visitor's email in the Mailable |
| Sending email before saving | A mail failure loses the enquiry | Save to the database first, then send |
| Showing a different message to bots | Bots learn they were caught and adapt | Return the same success message for honeypot hits |
Frequently Asked Questions
Do I need a package to build a Laravel contact form?
No. Everything in this tutorial uses features built into Laravel 13: Blade, routing, Form Requests, Eloquent, Mailables and the throttle middleware. Packages can add features such as disposable-email blocking, but the working form needs none of them.
Why save the message to the database if I also send an email?
Because email fails. A mail server can be down, a message can land in spam, or an inbox can be full. The database copy means no enquiry is ever lost, and it gives you a searchable record of every message your site receives.
What is the honeypot field for?
It is a hidden form field that real visitors never see or fill. Spam bots fill in every field they find, including hidden ones. If the honeypot has a value when the form is submitted, you know the submission came from a bot and you drop it quietly.
Should the email be sent in the background?
For a small site, sending it directly is fine. For a busy site, make the Mailable implement ShouldQueue and run a queue worker. The visitor gets an instant response, and the email is delivered by the worker a moment later.
Which Laravel version does this work with?
This tutorial is written for Laravel 13, which requires PHP 8.3 or newer. The same pattern works in Laravel 11 and 12 with no changes, and Laravel 13.45 — the current release — includes security fixes you should not skip. Read our note on the Laravel 13.45 security release to see why keeping Laravel updated matters.
Want a website with forms that work — and a team that builds them properly? We build custom Laravel websites and web applications for clients worldwide, including contact forms, dashboards, booking systems and e-commerce. See our custom web application service, compare Laravel vs WordPress for your business website, then contact us for a free quote and tell us what you need.
Need a Website or App Built for Your Business?
Brother IT Digital designs and builds fast, modern websites, e-commerce stores, and mobile apps for clients around the world — and tells you plainly what your project should cost before you commit.