Request class is used to represent an HTTP request coming into your application. It provides convenient methods for retrieving data from the request, such as query parameters, POST data, headers, and more
Import Request class:
use Illuminate\Http\Request;
Inject Request instance:
public function myMethod(Request $request)
{
// Your code here
}
1. Accessing Input Data-
To get input data (including both GET and POST data), you can use the input method:
<?php
$value = $request->input('key');
?>
You can also retrieve all input data using the all method:
<?php
$alldata = $request->all();
?>
2. Accessing Specific Input-
<?php
$name = $request->input('name');
?>
To get a specific input field, you can use the input method with the field name.
3. Checking for Existence of Input:
if ($request->has('name')) {
// Do something
}
4. Accessing Query Parameters:
$param = $request->query('param_name');
To access query parameters from the URL, use the query method.
5. Accessing Route Parameters:
$id = $request->route('id');
To access route parameters, use the route method with the parameter name.
6. Accessing Headers:
$contentType = $request->header('Content-Type');
7. Determining Request Method:
$method = $request->method();
8. Additional Information:
You can retrieve various information such as the request URL, full URL, IP address, user agent, etc., using methods like url(), fullUrl(), ip(), userAgent(), etc.