How to Create a Registration form with a Login System in Laravel

Do you think creating a registration form with a login system is a tedious task? Well, that might be true if you try to do it yourself. But with this cool artisan, creating a registration form with a login system is going to be so simple. If you don’t know it already, the artisan is the command-line interface included in Laravel that provides several helpful commands to help you build your app.

So, let’s get started!

Step 1. Create a Laravel project

To create a new project, run the following command. Here, laravel appli is the name of the project which can be modified.

$Composer create- project – prefer-dist laravel/laravel laravel-appli

Step 2. Run the Artisan command

This command creates authentication scaffolding with all the related files and routes.

$php artisan make: auth
Step 3. Run the migrate command

This command creates the users’ table in your database.

$php artisan migrate

Step 4. Customize the user table

While it is perfectly fine for you to use the table as it is, most often than not, you’d like to make some changes to suit the kind of app you’re building.

To customize it, open the migration file. Then, you can add or change the field. According to the change, make required modifications in RegisterController.php and resources/views/auth/register.blade.php files. Finally, run migrate command in terminal.

Following are the steps to add a username. You can modify the lines accordingly to add other subjects.

Step 1.

Open migration file create_users_table.php and add the following line.

$table->string(‘username’,191)->nullable()->unique();

Step 2.

Run migrate command in terminal.

$Php artisan migrate
Step 3.

Open RegisterController.php, add the following lines and save the file.

protected function validator(array $data)
{
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'username' => 'required|string|max:191|unique:users',
            'email' => 'required|string|email|max:191|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
}
protected function create(array $data)
{
    return User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
    ]);
}

Step 4.

Open User.php model, add the following code and save it.

protected $fillable = [
    'name', 'email', 'password','username',
];

Step 5.

Open resources/view/auth/register.blade.php file, add the follow codes below the name field and save.

//Username
    {{ old('username') }}
@if ($errors->has('username'))
    {{ $errors->first('username') }}
@endif

Step 6.

Open LoginController.php file, add the following code and save.

//import it above the class
use Illuminate\Http\Request;
//add the code just below the constructor function
protected function credentials(Request $request)
{
    /// this method is overriden form 
    //Illuminate\Foundation\Auth\AuthenticatesUsers; class
    $field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)? $this->username():'username';
    return [
        $field => $request->get($this->username()),'password' => $request->password,
    ];

}

Step 7.

Open login.blade.php file, add the following codes and save it.

//Only change the type of Email field from email to text and change label {{ old('email') }}

Step 8.

For pages you want the users to access only after login, add the following codes in respective controller files.

public function __construct()    
{        
     $this->middleware('auth');
}

Step 9.

To set custom session, add the following code.

protected function authenticated(Request $request, $user)
{
     // overriden from Illuminate\Foundation\Auth\AuthenticatesUsers; class
     //set session
     $request->session()->put('admin', 'admin');
     //redirect
     return redirect()->intended($this->redirectPath());
}

Step 5.  Run the app on the development server

Use the artisan serve command to run the app on the php development server.

php artisan serve

Step 5.  Test

Check the registration and login in local host.

http://localhost:8000 

So, this is how we create registration form with a login system in Laravel.

We genuinely hope that this guide was useful to you. Feel free to leave your doubts in the comments section. We would be happy to help.