Requests

Uploaded Files

Retrieve uploaded file from request

$file = $request->file('photo');
$file = $request->photo;

Get file path or extension

$path = $request->photo->path();
$extension = $request->photo->extension();

Store uploaded file with a randomly generated filename

// path where the file should be stored relative to
// the filesystem's configured root directory
$path = $request->photo->store('images'); 
// optional 2nd param to specify the filesystem disk
$path = $request->photo->store('images', 's3');

Store uploaded file and specify the name

$path = $request->photo->storeAs('images', 'filename.jpg');
$path = $request->photo->storeAs('images', 'filename.jpg', 's3');

See More: Laravel File Storage

Old Input

Retrieve input from the previous request

$username = $request->old('username');

Or use the old() helper

<input type="text" name="username" value="{{ old('username') }}">

See: Helpers
See: Forms

Check Existence

Determine if value(s) present

if ($request->has('name')) {
    //
}
// check if ALL values are present
if ($request->has(['name', 'email'])) {
    //
}
// if any values are present
if ($request->hasAny(['name', 'email'])) {
    //
}

Retrieve Partial Input

$input = $request->only(['username', 'password']);
$input = $request->only('username', 'password');
$input = $request->except(['credit\_card']);
$input = $request->except('credit\_card');

Dynamic Properties

Access inputs via properties.
If not found as an input, the route parameters will be checked.

$name = $request->name;

Input

Retrieve all the incoming request's input data as an array

$input = $request->all();

Retrieve all the incoming request's input data as a collection

$input = $request->collect();
// retrieve subset as collection
$request->collect('users')->each(function ($user) {
    // ...
});

See Helpers Retrieve user input (also gets values from query string)

$name = $request->input('name');
// with default value if none present
$name = $request->input('name', 'Sally');

Access array inputs

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

Retrieve all the input values as an associative array:

$input = $request->input();

Only retrieve values from the query string:

$name = $request->query('name');
// with default value
$name = $request->query('name', 'Helen');

Retrieve all the query string values as an associative array:

$query = $request->query();

#Boolean Input Values

Helpful for checkbox inputs or other booleans. Return true for 1, "1", true, "true", "on", and "yes".
All other values will return false

$archived = $request->boolean('archived');

Content Type

Return an array containing all the content types accepted by the request

$contentTypes = $request->getAcceptableContentTypes();

Boolean check for content types are accepted by the request

if ($request->accepts(['text/html', 'application/json'])) {
    // ...
}

Headers

$value = $request->header('X-Header-Name');
$value = $request->header('X-Header-Name', 'default value');
// determine if the request contains a given header
if ($request->hasHeader('X-Header-Name')) {
    //
}
// retrieve a bearer token from the Authorization header
$token = $request->bearerToken();

Client IP

$ipAddress = $request->ip();

Request Method

$method = $request->method();
// verify that the HTTP verb matches a given string
if ($request->isMethod('post')) {
    //
}

URL

Full URL for the incoming request

// URL without the query string
$url = $request->url();
// URL including query string
$urlWithQueryString = $request->fullUrl();
// append data to query string
$request->fullUrlWithQuery(['type' => 'phone']);

Path

The request's path information

$uri = $request->path();
// https://example.com/foo/bar --> foo/bar

#Match path to pattern

Verify that the incoming request path matches a given pattern

// \* is wildcard
if ($request->is('admin/\*')) {
    //
}

Determine if the incoming request matches a named route

if ($request->routeIs('admin.\*')) {
    //
}

Accessing Request

Get an instance of the current request by type-hinting the controller action or route closure

// controller action
class UserController extends Controller
{
    public function store(Request $request)
 {
        $name = $request->input('name');
    }
}
// closure
Route::get('/', function (Request $request) {
    //
});

See Routing

CSRF Protection

Laravel automatically generates a CSRF "token" for each active user session.
This token is used to verify that the authenticated user is the person actually making the requests. Get current session's token:

Route::get('/token', function (Request $request) {
    $token = $request->session()->token();
    $token = csrf\_token();
    // ...
});

POST, PUT, PATCH, or DELETE forms should include a hidden CSRF _token field in the form to validate the request.

<form method="POST" action="/profile">
    @csrf
    <!-- Equivalent to... -->
    <input type="hidden" name="\_token" value="{{ csrf\_token() }}" />
</form>

See Forms

Comments