How to Override Auth Register Method in Laravel 8?
Hi,
This article goes in detailed on How to override auth register function in laravel 8. I’m going to show you about laravel override register method. In this article, we will implement a laravel fortify custom register. In this article, we will implement a auth register function in laravel.
Sometime we need to overwrite or custom code for register method, so here i will give you very simple example how to overwrite auth default register function in laravel app.
You can see bellow default route for register method:
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
so, Basically you can create new method register and showRegistrationForm into your RegisterController and override auth method. let's add code as like bellow:
app/Http/Controllers/Auth/RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Write code on Method
*
* @return response()
*/
public function showRegistrationForm()
{
return view('register');
}
/**
* Write code on Method
*
* @return response()
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
$this->create($request->all());
return redirect("dashboard");
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
i hope it can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Two Factor Authentication using Email Tutorial
- How to Add Two Factor Authentication with SMS in Laravel?
- Laravel 8 Two Factor Authentication with SMS
- Laravel 8 Firebase Mobile Number (OTP) Authentication Tutorial
- Laravel 8 Firebase Web Push Notification Example
- Laravel 8 Factory Tinker Example Tutorial
- Laravel 8 Multi Auth (Authentication) Tutorial
- Laravel 8 Install Bootstrap Example Tutorial
- Laravel 8 Cron Job Task Scheduling Tutorial
- Laravel 8 Socialite Login with Google Account Example
- Laravel 8 Pagination Example Tutorial
- Laravel 8 Livewire CRUD with Jetstream & Tailwind CSS