Skip to content
Advertisement

Laravel 8 – Multi Auth with two diffirent registration form?

I’m using laravel breeze as auth scaffolding package I want to know How can I create two diffirent registration form for two User Types here is a simple explanation of hwat I want to achieve:

resources/auth/developer :

developer-register.blade.php

resources/auth/designer :

designer-register.blade.php

if the Visitor choose to register as “developer” it will display a diffirent form. and same thing for if the Visitor choose to register as “designer” it will display a diffirent form with fields.

I wish you understand what I want to achieve with this easy explanation.

Advertisement

Answer

Ok, so i’ve not used laravel/breeze myself (yet) but it shouldn’t be much different from doing it in standard Laravel!


Views

By default, it looks like the breeze scaffolding is going to hit a create() method on the RegisteredUserController which will return a single view like so:

RegisteredUserController.php

    /**
     * Display the registration view.
     *
     * @return IlluminateViewView
     */
    public function create()
    {
        return view('auth.register');
    }

You have a few options here:

  1. Replace this view with another
  2. Add some logic to change the view which is returned based on the request being made (you can inject a Request object into the route like any other)
    public function create(Request $request)
    {
        if ($request->has('developer')) {
            return view('auth.developer-register');
        } else {
            return view('auth.designer-register');
        }
    }
  1. Keep the original auth.register view and handle the logic in the blade template.

Registration

The forms on each of your registration pages will have an action that points to a controller route. This will likely be the RegisteredUserController within which you will find a store() method that handles the creation of a User model.


RegisteredUserController.php

    /**
     * Handle an incoming registration request.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpRedirectResponse
     *
     * @throws IlluminateValidationValidationException
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|confirmed|min:8',
        ]);

        Auth::login($user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]));

        event(new Registered($user));

        return redirect(RouteServiceProvider::HOME);
    }

As you can see, this store() method is handling the creation of a User model and then authenticating it before redirecting the user to the home route.

What you could do, is check the request for the the requested user type and then use a switch statement to change the type of use being created.

switch ($request->get('user_type'))
    case 'developer':
        $user = Developer::create([ /* add details here */ ]);
        break;
    case 'designer':
        $user = Designer::create([ /* add details here */ ]);
        break;

Auth::login($user);


I hope this will at least inspire you with your own solution!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement