Home » Laravel21 November 2016

Laravel: Login Register [Beginner Tutorial]

This beginner tutorial/article shows how you can create a simple/basic user login and registration application using Laravel. In previous Laravel article, I have described in detail about Creating simple CRUD application in Laravel. In the CRUD article, I have described about Laravel folder structure and artisan command line tool. Hence, in this article, we will directly move towards creating database and tables.

Create Database

Let’s suppose, that our database name is laravel.

Here’s the MySQL query to create database:

Database Configuration Settings

After you create your database, you need to enter your database name and database login credentials in Laravel’s configuration settings file. Laravel’s database configuration file is present at /path-to-laravel-root/config/database.php.

We will be using MySQL database. So, we will edit “mysql” settings (database name, database username, database password, etc.) in database.php file.

Here is the update done by my side. You have to update it according to your database settings.

To use database commands from Artisan Command Line Tool, you also need to update database settings in /path-to-laravel-root/.env file because Artisan fetches database settings from .env file. Here’s the updated .env file on my computer:

Creating Table

We will be using Laravel’s Migrations feature to create database table. Migrations files are present in folder your-laravel-root/database/migrations/. From Laravel 5.0.0 onwards, there is a default migration file to create users table. It’s named 2014_10_12_000000_create_user_table.php.

If it’s not present, then you can run the following artisan commands to create one:

This will create a new migration file in your-laravel-root/database/migrations/ directory. This migration file contains class CreateUsersTable.

Here is the updated CreateUsersTable class:

Run the following command to execute the migration class

The table users will be created on your database laravel.

Creating Model

From Laravel 5.0.0 onwards, there is a User Model class present by default at your-laravel-root/app/User.php.

If it’s not present there, then run the following command to create the Model class ‘User’:

The User model class will be created at your-laravel-root-folder/app/User.php.

You need to add the table name, primary key of the table, etc. in the User class.

Here’s the updated User class:


The route file is present at app/Http/routes.php. Here is the code to be put in routes.php for user login and registration:

user.account page is set as auth middleware. Laravel will automatically check for user login when this page is accessed. If the user is not logged in the he/she is redirected to login page. The login redirect path can be adjusted from App\Http\Middleware\Authenticate::handle().

Creating Controller

In Controller class, we write all the logics to fetch data from database table, process it and pass it to views. Controller clasess are saved in app/Http/Controllers directory.

Run the following code to create a UserController controller class.

This will create UserController class at app/Http/Controller/UserController.php. Using --resource will create basic CRUD routes in the User controller class. It will create empty functions named index, create, update, destroy, etc.

In controller functions we call our model class, fetch the data, process it, and pass the data to view file.

Here is the updated UserController class used for this tutorial’s application:


Views are html files present in laravel-root/resources/views directory. Laravel uses Blade templating engine in views which helps to use loops and if/else conditions like PHP in the view html file.

Laravel uses blade templating engine, so the views files should be named as viewname.blade.php.

For this tutorial, we will first create a master template where we include/define the title, header, content and footer. We then extend the master template for other pages of our application. We also display success and error flash message on master template.

Here is our master template (laravel-root/resources/views/layout/master.blade.php):

resources/views/user/index.blade.php will be extending master.blade.php template and then displaying user info if logged in. If user is not logged in then login and register link is displayed.




Download Source Code from GitHub

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.