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:
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
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 helpersisMethod('PUT')
-
IP & proxies:
ip()
(honourstrustedproxy
config) -
Headers:
header('Accept-Language')
-
Content negotiation:
wantsJson()
,accepts(['text/html','application/json'])
All straight from the core docs. Laravel
5 Validation—three flavours
-
Inline
The method comes from the
ValidatesRequests
trait in the base controller. Stack Overflow -
Form Request classes
Your new class gains
authorize()
+rules()
and auto-runs before the controller fires. Laravel -
Manual with the
Validator
facade—handy for CLI or queued jobs. Laravel News
FYI: Form Requests extend
Illuminate\Foundation\Http\FormRequest
, which itself extendsRequest
, 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