The Request object
Illuminate\Http\Request provides an object-oriented interface for working with the current HTTP request. Through it you can access form data, query strings, uploaded files, headers, and more.
Injecting the request into controllers
Type-hintIlluminate\Http\Request in a controller method and Laravel’s service container injects the current request instance automatically:
Using route parameters alongside the request
When a controller method also needs a route parameter, list theRequest type-hint first, then the route parameters:
Retrieving input
All input
Useall() to retrieve all input as an array. It works for both HTML forms and XHR requests:
A specific field
input() retrieves a value from the entire request payload, including the query string, regardless of the HTTP method:
Query string only
query() retrieves values exclusively from the query string:
input() searches both the request body and the query string, while query() only searches the query string. Use query() when you need to distinguish between POST data and URL parameters.Typed retrieval
Laravel provides helpers to retrieve input already cast to a specific type:Strictly typed retrieval
Thestring, integer, float and boolean methods cast the incoming input to that type. For example, ?count=abc is cast to 0 by the integer method, and ?name[]=taylor causes an “Array to string conversion” error in the string method. If you want to receive a default value instead of casting when the input is not of the expected type, pass strict: true.
integer and float return the default value when the input is not a valid integer or number, string returns the default value when the input is not a string or number, and boolean returns the default value when the input is not one of 1, true, on, yes, 0, false, off or no.
Retrieving UUIDs and ULIDs
You can retrieve input values that contain a UUID or ULID with theuuid and ulid methods. uuid returns a Ramsey\Uuid\UuidInterface instance, and ulid returns a Symfony\Component\Uid\Ulid instance. If no input value exists with the given name, null is returned.
InvalidArgumentException is thrown. We therefore recommend validating the input before calling these methods.
Retrieving model input
When using form requests, you can retrieve Eloquent models using identifiers contained in the request input. Themodel method returns null when the input is empty or no matching record exists, while the modelOrFail method throws an Illuminate\Database\Eloquent\ModelNotFoundException. When the input value is an array of identifiers, the models method returns an Eloquent collection of the matching records.
_id or _ids suffix and converts the remaining name to singular studly case. The model is resolved from your application’s Models namespace, falling back to the application’s root namespace when no matching model exists there.
Dynamic properties
You can access input values as properties directly on the request object:Subsets of input
Useonly() and except() to retrieve a subset of input:
Checking for input
Usehas() to determine whether a value is present in the request:
filled() to check that a value is present and not empty:
isNotFilled() when the value is absent or empty:
Inspecting the request
Path and URL
HTTP method
Retrieving request headers
Retrieving a header
Use theheader method to retrieve a request header. If the header is not present, null is returned. You may pass a second argument to specify a default value to return when the header is missing.
hasHeader method to check whether the request contains a given header.
Retrieving a bearer token
To retrieve a bearer token from theAuthorization header, use the bearerToken method. If no token is present, null is returned.
File uploads
Retrieving an uploaded file
Usefile() to retrieve an Illuminate\Http\UploadedFile instance:
hasFile():
Storing the file
Usestore() to save the file to a storage disk. The filename is generated automatically:
storeAs():
Example: handling a registration form
Configuring trusted hosts
By default, Laravel responds to every request it receives, regardless of the content of the HTTP request’sHost header. The Host header’s value is also used when generating absolute URLs to your application during a web request.
Typically, you configure your web server, such as Nginx or Apache, to send only requests matching a given hostname to your application. If you can’t customize your web server directly and need to tell Laravel to respond only to certain hostnames, enable the Illuminate\Http\Middleware\TrustHosts middleware.
To enable the TrustHosts middleware, call the trustHosts method in bootstrap/app.php. Use the at argument to specify the hostnames your application should respond to. Hostname strings are treated as regular expressions, and requests with any other Host header are rejected.
subdomains argument.
at argument.
at argument. In that case, the host defined in the app.url configuration value and all of its subdomains are trusted.
app.url and not its subdomains, pass false to the subdomains argument.
Next steps
Responses
Learn how to return views, redirects, and JSON from your controllers.