Laravel User Registration

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

Overview

In this section of our Laravel Build a Blog tutorial, we will create our User model and the initial Registration Controller. 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 .env 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 User 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 User.


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 User php model. Let's make a new directory app/Http/Models for storing all of our models. Let's also go ahead and create a new directory app/Http/Models/Auth. We will store all of our auth related models within this directory.

Now let's create our app/Http/Models/Auth/User.php file. We will extend the basic Eloquent model and just add some simple code to link to our user 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 User model, we need to create a view & controller, allowing users to register. Let's first create a new directory app/Http/Controllers/Auth. We will store all auth related controllers here.

Next let's create our app/Http/Controllers/Auth/RegisterController.php. We will extend the default Controller 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 resources/views/auth and file resources/views/auth/layout.blade.php. For now our parent layout blade will have 1 input parameter called page_title. We will set this within out controller function. Then within the body we will use @yield('content') 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 resources/views/auth/register. 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 url to our controller function. We will do so in routes/web.php. 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 User model and Registration Controller. 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.

nice
Added from Giphy.