Back to Blog
LaravelBest PracticesClean Code

Keep Your Laravel Controllers Clean with Form Requests

Asky Mohammed
Asky MohammedSoftware Engineer
4 min read

I just started using Form Requests in Laravel and wow — it really keeps your controllers neat! 😄

Instead of stuffing all validation logic in your controller, move it to a Form Request. It handles:

Validation rules
Authorization
Cleaner, more maintainable code

Here's a simple example of how I use it in my projects:

php
// Before: Controller with validation logic
public function store(Request $request) {
    $validated = $request->validate([
        'title' => 'required|string|max:255',
        'content' => 'required|string',
        'category_id' => 'required|exists:categories,id',
    ]);
    // ... controller logic
}

// After: Using Form Request
public function store(StorePostRequest $request) {
    // All validation is in StorePostRequest
    // ... controller logic stays clean
}
php
// app/Http/Requests/StorePostRequest.php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
    public function authorize(): bool {
        return auth()->check();
    }

    public function rules(): array {
        return [
            'title' => 'required|string|max:255',
            'content' => 'required|string',
            'category_id' => 'required|exists:categories,id',
        ];
    }
}

Start using Form Requests today and make your Laravel code cleaner and easier to maintain! 🚀

Share this article
Asky Mohammed

Written by Asky Mohammed

Software Engineer specializing in full-stack web development with Next.js, Laravel, PostgreSQL, Redis, and Docker. Building scalable software with clean architecture.

Get in touch for engineering projects →