Back

All about creating a new user in Laravel

Laravel

All about creating a new user in Laravel

In this article, we shed light on everything related to creating a new user in Laravel, where we should prepare Model that belongs to users, let it be called Users.

It is noted that Model is already prepared with installation of a project it can be used, but in the absence of it, it can be created through the command line.

Activating Migration in Laravel

After creating Model, we also add Migration to it so that the database can be managed, we can do that through the command line as follows:

php artisan make:model Users -m

The same applies to Model, Migration is often installed and ready for the users, modifications can be done, but in this case, the step of creating Model and Migration becomes unnecessary.

It also requires paying attention to Routes file which is in this route when creating a new project: routes/web.php

After that, a new path is created by creating a new user, we make another path in order to view login page.

How to manage projects in Laravel

Therefore, it is necessary to create a set of paths of type Post in order to be able to manage the project in question, as follows:

Route::view('/signup' , 'your.view.path')->name('auth.signup');Route::post('/signup' , '[email protected]')->name('auth.signup.post');Route::view('/login' , 'your.view.path')->name('auth.login');Route::post('/login' , '[email protected]')->name('auth.login.post');

With regard to Views, it is a purely design process, you can add everything you want in the design, it is important when we put an element we add action with it so that it goes to the right path.

The fields should also ideally be named as the fields of the database so that we can use the create function in Model, so that we can create a new user.

A Csrf Token field should also be added so that the error message 419 does not appear in Laravel page, the field can be added by typing @csrf as follows:

<form method="post" action="{{route('auth.signup.post')}}">       @csrf       <input name="name" type="text" />       <input name="email" type="email" />       <input name="password" type="password" />       <button type="submit">Signup</button></form>

First steps to creating a new user in Laravel

When we want to create a new user in Laravel, we can do this through the create function that exists with Model and is installed automatically with installation of Laravel, and in order to be able to use this feature, we must first activate a feature called Mass Assigment in Model, by adding this code to Model as follows:

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;

class User extends Model{

protected $fillable= ['email' , 'password' , 'name' , 'username' , '..' , '..']; //This Allow only this Fields to be Mass Assigned

protected $guarded= []; //Or This : Allow All Fields to be Mass Assigned

}

After that, you can use Create function in Model, we write commands that are needed to activate the process of creating a new user, in order for the process to be done correctly, we must first make sure that the database is installed to link it to the application in the correct way.

We should also do a Migrate for the existing files, we write the necessary set of codes in order to create a new user, it is assumed that the database previously contains the users table with email, password and username.

You can add the fields you want to add in the same way as before.

<?phpnamespace App\Http\Controllers; //Namespace The Fileuse Validator; //Use The Validator Facade to validate the requestuse Hash; // Use The Hash Facade to hash the passworduse Illuminate\Http\Request; // Use The Request Facade to handle form requestsuse App\User; //Use The User Modelclass AuthController extends Controller{//Proccess The signuppublic function postSignup(Request $r){    //Validate the request    $NewsletterEmailRules = [        'name' =&gt; 'required|min:2|max:255',        'email' =&gt; 'required|email|unique:users,email',        'password' =&gt; 'required',    ];    $ValidatorErros = [        'name.required' =&gt; 'Name is required' ,        'name.min' =&gt; 'Name should be at least 2 charachters',        'name.max' =&gt; 'Name should not be more than 255 charachters',        'email.required' =&gt; 'Please enter your email' ,        'email.email' =&gt; 'Your email is invalid',        'email.unique' =&gt; 'This email is already registred ',        'password.required' =&gt; 'Password is required',    ];    $validatedData = Validator::make( $r-&gt;all(), $NewsletterEmailRules, $ValidatorErros );    if($validatedData-&gt;fails()){        return back()-&gt;withErrors($validatedData-&gt;errors()-&gt;all());    }else{        $HashedPassword = Hash::make($r-&gt;password); //Make The Password Hashed        $User = User::create($r-&gt;except('_token'));        return back()-&gt;withSuccess('User has been created!');            }} 

}

It is noted that the previous code does not have many difficulties or strange things, most of them are basics of Laravel.