Laravel Request: Input, Validation & Tips (Guide 2025)

Himmat Regar 1 Jun 14, 2025, 6:17 AM
Laravel
Views 655
Blog Thumbnail

Laravel & the HTTP Request: A Hands-On Guide for 2025

If cookies are the “outgoing mail” of a web app, the Request is the overflowing inbox that lands on your desk every time a user hits Enter. Laravel’s Illuminate\Http\Request class wraps that raw HTTP envelope in a fluent, secure, PSR-7-friendly API, so extracting data is usually a one-liner instead of a wrestling match.
This guide (tested against Laravel 11 & 12) shows you how to open, filter, validate, and even extend that inbox—plus some production war stories to keep you out of trouble.


1 What actually is a Request?

Laravel boots a single, immutable instance of Illuminate\Http\Request for the current cycle. It extends Symfony’s Request, adds helper sugar (input()validate(), macros, etc.) and plugs into Laravel’s container so you can type-hint it almost anywhere. Laravel Laravel

Quick start — In any route/controller:

use Illuminate\Http\Request;

Route::post('/profile', function (Request $request) {
    $email = $request->input('email');
});

2 Retrieving input like a pro

Task One-liner
Any value (body, query, JSON) $request->input('key', $default)
Only query-string $request->query('page')
Only JSON payload $request->json('token')
Check existence / non-empty $request->has('file')$request->filled('name')
Accept all fields $request->all()
Whitelist / blacklist $request->only('title','body')$request->except('password')
Laravel merges body + query + JSON into one bag, so you rarely care where the data came from.  

TIP Trim & nullify
The global middleware TrimStrings and ConvertEmptyStringsToNull run before you touch input, saving you from " " and empty strings in DB columns. Readouble


3 Files & uploads

$file = $request->file('avatar'); // Illuminate\Http\UploadedFile
$path = $file->store('avatars', 's3');

Check existence with hasFile(), limit size in validation, and remember browsers cap multipart/form-data to ~2 GB. Laravel


4 Request metadata you’ll need sooner or later

  • Path / URL: path()url()fullUrl()

  • Method: method(), plus magic helpers isMethod('PUT')

  • IP & proxies: ip() (honours trustedproxy config)

  • Headers: header('Accept-Language')

  • Content negotiation: wantsJson()accepts(['text/html','application/json'])
    All straight from the core docs. Laravel


5 Validation—three flavours

  1. Inline

    $validated = $request->validate([
        'email' => 'required|email|ends_with:example.com',
    ]);
    

    The method comes from the ValidatesRequests trait in the base controller. Stack Overflow

  2. Form Request classes

    php artisan make:request StoreArticleRequest
    

    Your new class gains authorize() + rules() and auto-runs before the controller fires. Laravel

  3. Manual with the Validator facade—handy for CLI or queued jobs. Laravel News

FYI: Form Requests extend Illuminate\Foundation\Http\FormRequest, which itself extends Request, so you still get every helper.


6 Flashing & redirecting (old input)

return back()
   ->withInput()             // flashes current input
   ->withErrors($validator); // pairs well with validation

Laravel stashes the payload in the session so, after a redirect, Blade’s old('field') repopulates forms. Laravel


7 Merging & sanitising on the fly

Need to inject or overwrite a field mid-request?

$request->merge(['slug' => Str::slug($request->title)]);

merge() returns void—the original object is still immutable for everyone else who already has a reference. Laravel

Related Posts

What-is-laravel-controller
150 viewsLaravel
Himmat Kumar Jun 14, 2025, 3:40 AM

What is Laravel - Controllers

what-is-laravel-request
174 viewsLaravel
Himmat Kumar Jun 14, 2025, 3:30 AM

What is a request in Laravel?

mastering-laravel-middleware
260 viewsLaravel
Himmat Regar 1 Jun 14, 2025, 3:24 AM

Mastering Laravel Middleware: Registration, Customizati...

laravel-cookies-guide
575 viewsLaravel
Himmat Regar 1 Jun 14, 2025, 3:23 AM

Laravel Cookies: Secure, Encrypted & Easy (Guide 2025)

what-is-laravel-routing
112 viewsLaravel
Himmat Kumar Jun 14, 2025, 2:29 AM

What is laravel routing

get-route-in-laravel
106 viewsLaravel
Himmat Kumar Jun 14, 2025, 1:32 AM

GET Route in Laravel

eloquent-relationships-guide
598 viewsLaravel
Himmat Regar 1 Jun 14, 2025, 12:47 AM

Mastering Eloquent Relationships in Laravel (2025) — Co...

laravel-vs-cakephp-comparison
624 viewsLaravel
Himmat Regar 1 Jun 14, 2025, 12:47 AM

Laravel vs CakePHP (2025) — Which PHP Framework Is Best...

laravel-csrf-protection-guide
580 viewsLaravel
Himmat Regar 1 Jun 14, 2025, 12:47 AM

Laravel CSRF Protection Explained – Tokens, Middleware ...

laravel-setup-tutorial
183 viewsLaravel
Himmat Kumar Jun 14, 2025, 12:47 AM

Master Laravel: Basic System Requirement,Installation a...