0

Retrieving input — $request->input(), get(), query(), post()

Beginner5 min read·lv-09-002

Concept

Retrieving input from Laravel's Request object uses a consistent API for all input sources — query strings, form bodies, JSON bodies. The key insight is that input() merges all of these together.

$request->input(string $key, mixed $default = null): Get any input value — from GET query string, POST form body, OR JSON body. Input is merged from all sources. JSON bodies are automatically decoded. Pass a dot-notated key to access nested values in JSON: $request->input('user.address.city').

$request->get(string $key): Alias for input(). Same behavior.

$request->query(string $key): Get only from the query string (GET parameters). Ignores body.

$request->post(string $key): Get only from the POST body. Ignores query string.

$request->all(): Get all input as an array — query string + body merged. Use $request->all() cautiously — it includes all input, which may include fields you didn't expect.

$request->only(array $keys): Get only the specified keys. Useful for selectively extracting fields.

$request->except(array $keys): Get all input EXCEPT specified keys.

$request->validated(): Get only the validated input from a Form Request. Safer than all() — only includes declared fields.

$request->safe(): Get a ValidatedInput instance with only(), except(), all(), merge() methods. Returns only validated data.

$request->missing(string $key) / $request->has(string $key) / $request->filled(string $key): Check for key presence (has), non-null presence (filled), or absence.

Code Example

php
<?php
// Given: POST /search?type=active with body: {"name": "alice", "age": 25}

$request->input('type');       // "active" (from query string)
$request->input('name');       // "alice" (from body)
$request->input('missing');    // null
$request->input('missing', 'default'); // "default"

// Nested JSON input
// Body: {"user": {"address": {"city": "London"}}}
$city = $request->input('user.address.city'); // "London"
$user = $request->input('user');              // ['address' => ['city' => 'London']]

// Source-specific access
$request->query('type');       // "active" — only query string
$request->post('name');        // "alice" — only POST body

// Bulk retrieval
$all  = $request->all();                          // all merged input
$some = $request->only(['name', 'email']);         // only specified
$most = $request->except(['password', '_token']); // everything except these

// Presence checks
$request->has('name');      // true if key exists (even if null)
$request->filled('name');   // true if key exists AND is not empty
$request->missing('name');  // true if key does NOT exist
$request->isNotEmpty('age'); // true if key exists and has a non-empty, non-null value

// Multiple key check
$request->hasAny(['email', 'phone']); // true if either exists
$request->has(['name', 'email']);     // true if BOTH exist

// When helper — conditional logic based on input presence
$users = User::when($request->has('search'), function($query) use ($request) {
    $query->where('name', 'like', '%' . $request->search . '%');
})->get();

// Old input (flashed to session after validation failure)
$oldName = $request->old('name'); // repopulates form fields
// In Blade: <input value="{{ old('name') }}">