Laravel Request

In this topic, we will learn about Requests in Laravel.

Retrieving The Request Path

The path method returns the request's path information. So, if the incoming request is targeted at http://example.com/foo/bar, the path method will return foo/bar

$uri = $request->path();

The is method allows you to verify that the incoming request path matches a given pattern. You may use the * character as a wildcard when utilizing this method:

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

Retrieving The Request URL

To retrieve the full URL for the incoming request you may use the url or fullUrl methods. The url method will return the URL without the query string, while the fullUrl method includes the query string:

// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();

Retrieving The Request Method

The method method will return the HTTP verb for the request. You may use the isMethod method to verify that the HTTP verb matches a given string:

$method = $request->method();
if ($request->isMethod('post')) {
 //
}

Retrieving All Input Data

You may also retrieve all of the input data as an array using the all method:

$input = $request->all();

Retrieving An Input Value

the input method may be used to retrieve user input:

$name = $request->input('name');

We may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

$name = $request->input('name', 'Peter');

We may call the input method without any arguments in order to retrieve all of the input values as an associative array:

$name = $request->input();

Retrieving Input From The Query String

While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string:

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

If the requested query string value data is not present, the second argument to this method will be returned:

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

We may call the query method without any arguments in order to retrieve all of the query string values as an associative array:

$name = $request->query();

Retrieving Input Via Dynamic Properties

We may also access user input using dynamic properties on the Illuminate/Http/Request instance. For example, if one of your application's forms contains a name field, you may access the value of the field like so:

$name = $request->name;

Retrieving A Portion Of The Input Data

If you need to retrieve a subset of the input data, you may use the only and except methods. Both of these methods accept a single array or a dynamic list of arguments:

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

$input = $request->only('username', 'password');

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

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

Determining If An Input Value Is Present

we should use the has method to determine if a value is present on the request. The has method returns true if the value is present on the request:

if  ($request->has('name')) {
  //
}

When given an array, the has method will determine if all of the specified values are present:

if  ($request->has(['name', 'email'])) {
  //
}

The hasAny method returns true if any of the specified values are present:

if  ($request->hasAny(['name', 'email'])) {
  //
}

If you would like to determine if a value is present on the request and is not empty, you may use the filled method:

if  ($request->filled('name')) {
  //
}

To determine if a given key is absent from the request, you may use the missing method:

if  ($request->missing('name')) {
  //
}

Retrieving Old Input

To retrieve flashed input from the previous request, use the old method on the Request instance. The old method will pull the previously flashed input data from the session:

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

Retrieving Cookies From Requests

To retrieve a cookie value from the request, use the cookie method on a Illuminate/Http/Request instance:

$value = $request->cookie('name');

Alternatively, we may use the Cookie facade to access cookie values

use Illuminate/Support/Facades/Cookie;
$value = Cookie::get('name');

Retrieving Uploaded Files

We may access uploaded files from a Illuminate/Http/Request instance using the file method or using dynamic properties.

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