Home >PHP Framework >Laravel >Laravel applications deal with users, starting from getting their data!
The following tutorial column will introduce you to the Laravel application and dealing with users, starting from getting their data! , I hope it will be helpful to friends in need! Introduction
, so in order to give backend developers some buffering time, we skip the view and first talk about the acquisition and processing of user data. This is almost a must-have function for any application. .
User data is alsodangerous
Code time
, and are publicly accessible for you to come and go.
If it is the private data of a single user, it must not be directly transmitted inline with the URL.
laravelStore user input in the
Input object, and logically, user input should belong to the request item, so Request Also inherits the methods and data of Input. For example, the following route: Route::post('form', function (Illuminate\Http\Request $request) {
// $request->method()});
simulates a form address, transmits data through the post method, and then uses the Request method. Let’s take a look at the available acquisition methods.
Let’s talk about
$request->all()first. This one prints all the input data. For example, the form may have the following fields. Just take a look at the HTML content!
In order to debug data, our route is registered like this: Route::post('/post-route', function (Request $request) {
var_dump($request->all());});
Guess what will be output?
/** * [ * '_token' => 'CSRF token here', * 'firstName' => 'value', * 'utm' => 12345 * ] */
Yes, that is the field firstName of the post form, the query parameter utm of querystring, and the laravel built-in function for CSRF protection. The default form field is
__token, so don’t bother.
But students who are obsessed with code may find it unbearable. This is not my field, and I don’t use it in the code. I don’t want to see this __token, this unfamiliar variable. Yes, we can just filter it out directly in the request parameters:
Route::post('/post-route', function (Request $request) { var_dump($request->except('_token'));});
Among them, the except() method is used to filter certain fields and does not need to be used. Corresponding to this is the
only() method, which specifies which fields are used. Use it like this: $request->only(['firstName', 'utm']);
Sometimes it is logical to determine whether some keys exist. Laravel provides two ways to determine whether they exist, one is has and the other is
. Some classmates may wonder, why do we still need exists when we have has? Because their functions are different. hasThe method will not only determine that the key exists, but if the value of the key is
empty (such as null, empty string, false, 0, empty array, empty object, etc.), is also judged as false. Therefore, it is necessary to use exists to simply determine whether a key exists or not. You must pay attention to this subtle difference when you use it. Usage is very simple, just call the method directly: if ($request->has('utm')) {
// Do some work}
If the input value key does not exist, we can also set a default value for it, this is
The second parameter of the method is called as follows:
Route::post('/post-route', function (Request $request) { $userName = $request->input('name', 'anonymous');});
If the user does not pass in the name field, then use anonymous instead. The above are all simple key-value pairs. There is also a form. The incoming fields may be arrays, such as the following:
You can use dot style to obtain arrays in laravel. method to read, this is because laravel uses the general method of the helper class Arr when parsing. For example, to get a certain key:
$employeeZeroFirstName = $request->input('employees.0.firstName');
Clearly specify the key name and correspond to the level. You can also use asterisks to match all keys at a certain level:
$allLastNames = $request->input('employees.*.lastName');
Or just specify a certain key, and then return it as is regardless of how many levels there are:
$employeeOne = $request->input('employees.1');
For specific usage, readers can check the source code or documentation. Let’s delve into the usage of the
Arroperation class.
This article almost covers some of the most commonly used methods for user input. We only talked about how to obtain user data normally, It doesn't talk about how to verify data validity. Because the front-end verification is almost useless, the final data written to the database still needs to be checked by the application.
That is the job of verifier. We will discuss the verifier in detail in subsequent chapters.
The above is the detailed content of Laravel applications deal with users, starting from getting their data!. For more information, please follow other related articles on the PHP Chinese website!