Home > PHP Framework > Laravel > Laravel applications deal with users, starting from getting their data!

Laravel applications deal with users, starting from getting their data!

藏色散人
Release: 2020-10-28 14:56:41
forward
2025 people have browsed it

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

After talking about routing and controllers, we should talk about views next, but since the function of views is too boring, and it is a front-end page, it is inevitable. Write some js and css. Laravel is

focused on the backend

, 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 also

dangerous! This article will not teach you how to avoid danger!

Code time

When we were talking about routing planning, we talked about how to use the positional parameter binding of the URL for guidance. In fact, it is also a way to obtain users. The way to input data, is just that the incoming location parameters are generally

harmless to humans and animals

, 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.

laravel

Store 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()});
Copy after login
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!

Copy after login
Copy after login
    {{ csrf_field() }}         

In order to debug data, our route is registered like this:

Route::post('/post-route', function (Request $request) {
    var_dump($request->all());});
Copy after login
Guess what will be output?

/**
* [
* '_token' => 'CSRF token here',
* 'firstName' => 'value',
* 'utm' => 12345
* ]
*/
Copy after login

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'));});
Copy after login

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']);
Copy after login
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

exists

. 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}
Copy after login
If the input value key does not exist, we can also set a default value for it, this is

input

The second parameter of the method is called as follows:

Route::post('/post-route', function (Request $request) {
    $userName = $request->input('name', 'anonymous');});
Copy after login

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:

Copy after login
Copy after login
    {{ csrf_field() }}                         

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');
Copy after login

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');
Copy after login

Or just specify a certain key, and then return it as is regardless of how many levels there are:

$employeeOne = $request->input('employees.1');
Copy after login

For specific usage, readers can check the source code or documentation. Let’s delve into the usage of the

Arr

operation class.

Written at the end

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template