How to Build a Simple Contact Form in Laravel

CodeWithSdCodeCodeWithSdCode
2 min read

Introduction

Building a contact form is a common task when creating websites. In this tutorial, I will guide you step-by-step on how to create a simple and secure contact form using Laravel.

Step 1: Create a Route

Add this route in your routes/web.php file:

use App\Http\Controllers\ContactController;

Route::get('/contact', [ContactController::class, 'showForm'])->name('contact.form');
Route::post('/contact', [ContactController::class, 'submitForm'])->name('contact.submit');

Step 2: Create a Controller

Run this command in terminal to create a controller:

php artisan make:controller ContactController

Then, open app/Http/Controllers/ContactController.php and add:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function showForm()
    {
        return view('contact');
    }

    public function submitForm(Request $request)
    {
        $request->validate([
            'name'    => 'required|string|max:255',
            'email'   => 'required|email',
            'message' => 'required|string',
        ]);

        // Here you can handle the form data, like saving or sending email

        return back()->with('success', 'Thank you for contacting us!');
    }
}

Step 3: Create a Blade View

Create a new file resources/views/contact.blade.php and add:

<!DOCTYPE html>
<html>
<head>
    <title>Contact Us</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h2>Contact Us</h2>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    @if($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach($errors->all() as $error)
                  <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('contact.submit') }}" method="POST">
        @csrf
        <div class="mb-3">
            <label>Name</label>
            <input type="text" name="name" class="form-control" value="{{ old('name') }}">
        </div>
        <div class="mb-3">
            <label>Email</label>
            <input type="email" name="email" class="form-control" value="{{ old('email') }}">
        </div>
        <div class="mb-3">
            <label>Message</label>
            <textarea name="message" class="form-control" rows="4">{{ old('message') }}</textarea>
        </div>
        <button type="submit" class="btn btn-primary">Send</button>
    </form>
</div>
</body>
</html>

Step 4: Validate and Handle Form Submission

This is already included in the submitForm method inside your ContactController. Laravel automatically validates inputs and sends back errors if anything is wrong.

Conclusion

You now have a fully working contact form in Laravel!

  • Visit /contact URL to see your form.

  • Submit the form and see validation in action.

  • Add your own logic to send emails or save data.

1
Subscribe to my newsletter

Read articles from CodeWithSdCode directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CodeWithSdCode
CodeWithSdCode

I’m SdCode, a passionate Laravel developer sharing simple tutorials and practical coding tips to help beginners and intermediate devs grow their skills and build great projects.