Way To Override Or Change Laravel 8 Default Register Method

Hello artisan,

Have you faced such a situation that you want to override the Laravel 8 default auth register method but you don't know how to override the Laravel 8 default register method? So if don't know then don't worry. I am here to show you the simple way of overriding the Laravel 8 default register method.

Suppose you need to send a welcome email after registration or something else, So you want to change the default register method. So if you want to override this register method, we need showRegistrationForm and register method.

See the below example and steps to change the register method:

Laravel provides those two routes for doing registration.

routes/web.php

Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

 

So we need a RegisterController controller inside auth folder to override the register method in Laravel 8.

app/Http/Controllers/Auth/RegisterController.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']),
        ]);
    }
}

 

Read also: Create Custom Two Factor Authentication System in Laravel

 

Hope it can help you.

 

#laravel #laravel-8x