Important things when creating a new user in Laravel
In this article, we will shed light on the important things when creating a new user in Laravel, these things include three points that can be interesting for developers, it helps in preparing projects distinctly.
We will talk about these things in detail, to show their importance when creating a new user.
1 - Encrypt the password
It is used in Laravel framework of Facade called Hash in order to create encrypted passwords, it is generally used to do encryption, to be used, this can be done by calling it using the code: use Hash, regarding the actual use of this code, this is done as follows:
$HashedPassword = Hash::make($r->password);
Thanks to this code, there is a $HashedPassword variable for the password that a user enters, but in an encrypted way so that it can be used to log in.
This is very important when creating a new user, it is excellent so that passwords are saved in an encrypted way instead of being saved as texts directly in the database.
2 - Confirm that the entries are correct
We can always confirm that the input is valid by using Validator property at the beginning of the use Validator code, then using it when filtering the input.
One of the important things when creating a new user, the wonderful thing that we notice in Laravel, is that we can enter a code that allows the email not to be repeated, through the following line:
'email' => 'required|email|unique:users,email',
It is also noted, that the field for the email property does not allow the possibility of recurrence, if a previously registered email is to be entered, the error message will appear as follows:
'email.unique' => 'Email is already registered ',
3 – Using the create function in Model
The create function in Model is one of the most unique functions in Laravel, its work can be summarized as follows:
Model's create function compares Request to the fields in the database, and then adds each field to the matching fields in a pretty cool way.
All you have to do is to name the added fields with the same names as the fields in the database, then the matching can easily be done automatically.
Other settings to check
Despite this great feature, you must first check Mass Assigment and set it to allow mode, otherwise it will show an error message.
Now, we have finished showing the important matters when creating a new user, the process has been completed successfully, so we can do a set of system improvements with ease.
It is possible to add a welcome e-mail with the registration of each new user immediately after saving it in the databases.
Notifications can also be sent to the site manager with the presence of a new user, and many other requirements that may arise while working on a project.