> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTP requests

> Access form data, query strings, files, and more using Laravel's Illuminate\Http\Request object.

## 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-hint `Illuminate\Http\Request` in a controller method and Laravel's service container injects the current request instance automatically:

```php theme={null}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request): RedirectResponse
    {
        $name = $request->input('name');

        // Save the user...

        return redirect('/users');
    }
}
```

Route closures work the same way:

```php theme={null}
use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
    // ...
});
```

### Using route parameters alongside the request

When a controller method also needs a route parameter, list the `Request` type-hint first, then the route parameters:

```php theme={null}
use App\Http\Controllers\UserController;

Route::put('/user/{id}', [UserController::class, 'update']);
```

```php theme={null}
public function update(Request $request, string $id): RedirectResponse
{
    // Access request data via $request, route parameter via $id

    return redirect('/users');
}
```

## Retrieving input

### All input

Use `all()` to retrieve all input as an array. It works for both HTML forms and XHR requests:

```php theme={null}
$input = $request->all();
```

### A specific field

`input()` retrieves a value from the entire request payload, including the query string, regardless of the HTTP method:

```php theme={null}
$name = $request->input('name');
```

Supply a default value as the second argument:

```php theme={null}
$name = $request->input('name', 'Sally');
```

Access nested array input with dot notation:

```php theme={null}
$name = $request->input('products.0.name');

$names = $request->input('products.*.name');
```

### Query string only

`query()` retrieves values exclusively from the query string:

```php theme={null}
$name = $request->query('name');

$name = $request->query('name', 'Helen');

// All query string values as an array
$query = $request->query();
```

<Info>
  `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.
</Info>

### Typed retrieval

Laravel provides helpers to retrieve input already cast to a specific type:

```php theme={null}
// 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');
```

### Dynamic properties

You can access input values as properties directly on the request object:

```php theme={null}
$name = $request->name;
```

<Tip>
  Dynamic properties first search the request payload, then fall back to route parameters if nothing is found.
</Tip>

### Subsets of input

Use `only()` and `except()` to retrieve a subset of input:

```php theme={null}
$input = $request->only(['username', 'password']);

$input = $request->except(['credit_card']);
```

## Checking for input

Use `has()` to determine whether a value is present in the request:

```php theme={null}
if ($request->has('name')) {
    // ...
}

// Check that all specified keys are present
if ($request->has(['name', 'email'])) {
    // ...
}
```

Use `filled()` to check that a value is present and not empty:

```php theme={null}
if ($request->filled('name')) {
    // ...
}
```

Use `isNotFilled()` when the value is absent or empty:

```php theme={null}
if ($request->isNotFilled('name')) {
    // ...
}
```

## Inspecting the request

### Path and URL

```php theme={null}
// 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 route
if ($request->routeIs('admin.*')) {
    // ...
}

// Full URL without the query string
$url = $request->url();

// Full URL including the query string
$urlWithQueryString = $request->fullUrl();
```

### HTTP method

```php theme={null}
$method = $request->method();

if ($request->isMethod('post')) {
    // Only process POST requests
}
```

## File uploads

### Retrieving an uploaded file

Use `file()` to retrieve an `Illuminate\Http\UploadedFile` instance:

```php theme={null}
$file = $request->file('photo');

// Or as a dynamic property
$file = $request->photo;
```

Check that a file was actually uploaded with `hasFile()`:

```php theme={null}
if ($request->hasFile('photo')) {
    // ...
}
```

### Storing the file

Use `store()` to save the file to a storage disk. The filename is generated automatically:

```php theme={null}
// Store in the "images" directory on the default disk
$path = $request->photo->store('images');

// Store on S3
$path = $request->photo->store('images', 's3');
```

To specify a filename, use `storeAs()`:

```php theme={null}
$path = $request->photo->storeAs('images', 'filename.jpg');
```

<Warning>
  Always validate the file type and size when accepting uploads. Use Laravel's validation rules to handle this safely.
</Warning>

## Example: handling a registration form

```php theme={null}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
    public function store(Request $request): RedirectResponse
    {
        // Retrieve individual fields
        $name = $request->input('name');
        $email = $request->input('email');

        // Or retrieve only the fields you need
        $data = $request->only(['name', 'email', 'password']);

        // Create the user...

        return redirect('/dashboard');
    }
}
```

Define the corresponding route:

```php theme={null}
use App\Http\Controllers\RegisterController;

Route::post('/register', [RegisterController::class, 'store']);
```

## Next steps

<Card title="Responses" icon="arrow-right" href="/en/responses">
  Learn how to return views, redirects, and JSON from your controllers.
</Card>
