Laravel Tutorial · Requests and Responses · Lesson 8 of 27
Laravel Requests and Input
Read query strings, form fields, JSON bodies, headers and files from the Request object, with defaults and type helpers.
The Request object
Type-hint Illuminate\Http\Request in a route or controller and Laravel injects the current request. The most useful methods:
| Method | Returns |
|---|---|
$request->input('name', 'default') |
A value from the query string or body (form or JSON); dot notation for nested data |
$request->query('page') |
Only from the query string |
$request->only(['a', 'b']) / except([...]) |
A subset of the input |
$request->has('x'), filled('x'), missing('x') |
Presence checks |
$request->integer('qty'), boolean('agree'), date('from') |
Typed values |
$request->header('X-Api-Key'), bearerToken() |
Headers |
$request->file('avatar') |
An uploaded file |
$request->method(), path(), url(), ip() |
Request details |
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::post('/orders', function (Request $request) {
return [
'product' => $request->input('product'),
'qty' => $request->integer('qty', 1),
'gift' => $request->boolean('gift'),
'city' => $request->input('address.city', 'unknown'), // dot notation for nested input
'coupon_given' => $request->filled('coupon'),
'page' => $request->query('page'),
'only' => $request->only(['product', 'qty']),
'token' => $request->bearerToken(),
];
});
visit('POST', '/orders?page=2', [
'product' => 'Kettle', 'qty' => '3', 'gift' => 'yes', 'coupon' => '',
'address' => ['city' => 'Lisbon'],
], ['Authorization' => 'Bearer abc123']);
Output
POST /orders?page=2 -> 200 {"product":"Kettle","qty":3,"gift":true,"city":"Lisbon","coupon_given":false,"page":"2","only":{"product":"Kettle","qty":"3"},"token":"abc123"}Run this on your computer — see “Get Started” for setup.
Never pass $request->all() straight into a database create: validate first (next page) and use the validated data.
Test yourself
Which method reads a value only from the query string?
$request->input('page')$request->query('page')$request->header('page')$request->file('page')
Show answer
Answer: $request->query('page')