What is a Request in Laravel?
Within the context of a Laravel application, using a Request object, all the information returned by a client to a server in the form of an HTTP request can be accurately obtained. This includes all the information sent by the client including input data, headers, and configurations. Requests are okay for submitting forms, making API requests, or even more generally, interacting with the server.
Types of Requests
- GET: This retrieves data from the server.
- POST: Used when there is a need to send information to the server (such as during the submission of forms).
- PUT: This helps to change existing information.
- DELETE: This deletes information from the server.
Accessing Request Data
Laravel provides various methods to access the data in a request:
- Using the
input()
method:$request->input('key')
- Directly accessing query parameters:
$request->query('key')
- Retrieving all input data:
$request->all()
Request Validation
Validation ensures that the incoming data meets specific criteria. Laravel easily validate request using predefine validation in laravel using validate method:
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email',
'password' => 'required|min:8',
]);
Handling File Uploads
Laravel simplifies file uploads with the file()
and store()
methods:
if ($request->hasFile('photo')) {
$path = $request->file('photo')->store('photos');
}
Summary
The Request object in Laravel is one of the most important classes focusing on the HTTP aspects of the application. It offers convenient functionalities to get, filter and edit the request data which simplifies the building of flexible and safe web applications.