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.
When a controller method also needs a route parameter, list the Request type-hint first, then the route parameters:
use App\Http\Controllers\UserController;Route::put('/user/{id}', [UserController::class, 'update']);
public function update(Request $request, string $id): RedirectResponse{ // Access request data via $request, route parameter via $id return redirect('/users');}
query() retrieves values exclusively from the query string:
$name = $request->query('name');$name = $request->query('name', 'Helen');// All query string values as an array$query = $request->query();
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.
Laravel provides helpers to retrieve input already cast to a specific type:
// Returns a Stringable instance$name = $request->string('name')->trim();// Returns an integer$perPage = $request->integer('per_page');// Returns a boolean ("1", "true", "on", "yes" all become true)$archived = $request->boolean('archived');// Returns an array$versions = $request->array('versions');// Returns a Carbon instance$birthday = $request->date('birthday');
// Retrieve the path (e.g. "foo/bar")$uri = $request->path();// Test the path against a pattern (* wildcard supported)if ($request->is('admin/*')) { // ...}// Test against a named routeif ($request->routeIs('admin.*')) { // ...}// Full URL without the query string$url = $request->url();// Full URL including the query string$urlWithQueryString = $request->fullUrl();
Use store() to save the file to a storage disk. The filename is generated automatically:
// Store in the "images" directory on the default disk$path = $request->photo->store('images');// Store on S3$path = $request->photo->store('images', 's3');