Laravel User Registration

August 2020 | John Binzak
Learn how to create a user registration flow from scratch in Laravel.

Overview

In this section of our Build a Blog tutorial, we will create our model and the initial . Laravel has tools help you generate basic user auth (login/register) for your app without having to write code from scratch. However in this tutorial we will be creating the auth from scratch, in order to better understand how everything works.

Create a Database

If you have not already created a database for your project, let's do so now. Below you will find some basic SQL to create a database and user for our project. After you run the SQL, you will also need to set the variables in your file.


CREATE DATABASE laravel_social;
USE laravel_social;

CREATE USER 'luser'@'127.0.0.1' IDENTIFIED BY 'password123';
GRANT CREATE,SELECT,INSERT,UPDATE,ALTER,DELETE ON laravel_social.* TO 'luser'@'127.0.0.1';
-

User Model

Laravel has tools help you generate basic user auth (login/register) for your app without having to write code from scratch. However in this tutorial we will be creating the auth from scratch, in order to better understand how everything works.

First we will create our model. We will keep it very simple with the main fields being email & password. We will also include a verification token for an email verification feature. Below you will find the basic SQL for our .


CREATE TABLE user (
id mediumint(9) NOT NULL AUTO_INCREMENT,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
verification_token VARCHAR(255) DEFAULT NULL,
verified_at TIMESTAMP DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id));
-

Next we will init our php model. Let's make a new directory for storing all of our models. Let's also go ahead and create a new directory . We will store all of our auth related models within this directory.

Now let's create our file. We will extend the basic model and just add some simple code to link to our table and to identify the date fields.


namespace App\Http\Model\Auth;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class User
 * @package App\Http\Model
 * @property int id Primary key
 * @property string email
 * @property string password
 * @property string verification_token
 * @property Carbon verified_at
 * @property Carbon created_at
 * @property Carbon updated_at
 *
 */
class User extends Model {

    protected $table = 'user';

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['verified_at', 'created_at', 'updated_at'];
}

Register Controller

Now that we have our model, we need to create a view & controller, allowing users to register. Let's first create a new directory . We will store all auth related controllers here.

Next let's create our . We will extend the default provided by Laravel and create one function to show our view.


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RegisterController extends Controller{

    private $data = [];

    function index(Request $request){

        // set page title
        $this->data['page_title'] = 'Register';

        // return view & data
        return view('auth/register', $this->data);
    }

}

Register View

Since we will have many auth related views that will share common HTML code, we will create an auth parent layout blade. Let's create a directory and file . For now our parent layout blade will have 1 input parameter called . We will set this within out controller function. Then within the body we will use to display the child content.


    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>{{$page_title}}</title>
    </head>
    <body>
    @yield('content')
    </body>
    

Next let's build our register form in a new file . Here we will extend the parent auth blade and include our form HTML.


@extends('auth.layout')

@section('content')
<div class="w-50">
    <form>
        <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" id="password" placeholder="Password">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>
@stop
 

Register Route

We need to now route our desired to our controller function. We will do so in . Let's add the following 2 lines, allowing GET & POST traffic to our register function.


// register
Route::get('/register', 'RegisterController@index');
Route::post('/register', 'RegisterController@index');

Summary

Now we have our basic model and . We will use this design pattern for other parts of the app. Next we will complete the registration process as well as include email verification. You can find the source code here.