[Solved] Override Auth Register Method in Laravel 8

Hi,

This examplr goes in detailed on How to override auth middleware register(signup) function in laravel 8. I’m going to show you about laravel override register(signup) method. In this tutorial, we will implement a laravel custom register(signup). In this tutorial, we will implement a auth register(signup) function in laravel.

Sometime we need to overwrite or custom code for register(signup) 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', 'User\SignUpController@showSignUpForm')->name('signup');
Route::post('register', 'User\SignUpController@signup');

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/User/SignController.php

<?php
  
namespace App\Http\Controllers\User;
  
use App\Http\Controllers\BaseController;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
  
class SignController extends BaseController
{


    use RegistersUsers;
  
    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 showSignUpForm()
    {
        return view('signup');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function signup(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, [
            'firas_name' => ['required', 'string', 'max:500'],
            '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([
            'firas_name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

 We team hope it can help you….

Read Also Below Tutorials

Leave a Comment