Custom Registration with Laravel 5

by Sam  


Laravel provides registration and login authentication automatically using php artisan make:auth command. But sometimes,we need to add more data in our registration form, that’s why, we need to make our own registration and login form.

In this tutorial, I will show you how to make a custom registration using Laravel 5. This code works in Laravel 5.4 and 5.5. You can try this on other versions and please comment below how it works in you.

Let’s get started


1. Install Laravel

Open your terminal and run the command below.

 composer create-project --prefer-dist laravel/laravel registration 

After downloading,

 cd registration

2. Database connection

Create a database lara55db. Open .env file and change the settings accordingly.


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lara55db
DB_USERNAME=root
DB_PASSWORD=

Next,

 php artisan migrate 

Now, if you encounter an error like SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes, go to App>>Provider>>AppServiceProvider.php and add the code below:


use Illuminate\Support\Facades\Schema;

public function boot()
{
Schema::defaultStringLength(191);
}



3. Let’s make laravel Auth

Open your terminal and execute this command.

 php artisan make:auth 

4. Create AuthController

 php artisan make:controller AuthController 

The command above will create AuthController inside app>>Http>>Controllers. Now, copy the code below and paste inside the class.


/* GET
*/
public function registrationForm()
{
return view('custom_auth.register');
}

/* POST
*/
public function registerUser(Request $request)
{
$validate = \Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users|max:255',
'password' => 'required|confirmed|max:255'
]);
if( $validate->fails()){
return redirect()
->back()
->withErrors($validate);
}
$user_create = \App\User::create([
'password' => bcrypt($request->password),
'email' => $request->email,
'name' => $request->name
]);
return redirect('/register')->with('success', 'Successfully registered');
}

5. Change Routes

Go to routes>>web.php:

First, let us remove Auth::routes(); since we’re going to create our own route for registration.

Next, copy the code below.


Route::get('/register', 'AuthController@registrationForm');
Route::post('/register', 'AuthController@registerUser');



6. Create folder and files for Views

Next, we need to create registration form inside our views folder.

We added custom_auth folder, register.blade.php and customapp.blade.php. Take a look at the structure below:

Create registration in views

Now, copy the code below inside customapp.blade.php


<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> @yield('title') - {{ config('app.name') }}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>

Next, open your register.blade.php and copy the code below.


@extends('layouts.customapp')
@section('title', 'Registration Form')
@section('content')
<div class="container">
<div class="panel-heading">Custom Registration</div><hr>
@if( session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if ($errors->any())
<div class="alert alert-danger">
{!! implode('', $errors->all(':message <br>')) !!}
</div>
@endif
<form class="form-horizontal" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" autofocus>
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
@endsection

That’s all!

Start your artisan and go to http://localhost:8000/register

php artisan serve

What’s next?

You can now proceed to Login and Logout in Laravel 5



Leave a Reply

Your email address will not be published. Required fields are marked *


Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote