Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joker's avatar

How to register user ? Use Polymorphic Relationships in Laravel5

My project have a couple of different user types, with each type requiring different data ,I'm found one article about it,but nothing about register. http://webdevtuts.co.uk/polymorphic-relationships-in-laravel/

0 likes
9 replies
martinbean's avatar

@joker What do you want to do? What have you tried so far? You’re not really giving us much to go on.

joker's avatar

@matinbean,Sorry, my English is bad, can express a probably,I don't understand userable_id the meaning of this field,These problems cause I can't carry out, I searched a few examples, are written data in advance

martinbean's avatar

@joker You would have a base users table that is polymorphic, and contain user_id and user_type columns. You would then have models and tables for each types of user, maybe managers.

So if you have a manager with a primary key value of 1, then there would be a corresponding record in your users table whose user_id column is 1 and user_type column is something like App\Manager.

pdechery's avatar

Hi @martinbean,

Even this being a 7-month-old thread, would you (or anyone else) mind to explain how a register/login process might be on this type of situation?

My project have three different types of users and I'm not sure about how to handle the registration/login. I have one 'users' table which is polymorphic exactly how you've described. And I have also a model for each user type.

What I don't figure out right now is if I'll need to have a separate register/login page for each user. How would be the best way to do this?

Many thanks in advance.

Pierre

martinbean's avatar

@pdechery Would depend on how your application works. Does it have a single register page? Or would there be different registration forms to create different types of accounts?

1 like
pdechery's avatar

At this point I have a single register page, but since it's my first Laravel project I have doubts if it's the best way.

What I have in mind is a register page that, right after the registering part, takes the user to a profile page where he will fill his profile data.

But like I've said, I have three different users and each one should have its own profile page. So, with one single registration page that would be possible? And I could use the default Auth controller for that?

Thanks a lot.

OBS: I'm starting to think a 'many-register-pages' approach would be better...

Snapey's avatar

I only have two type of user, but this might be of use. The first type was a sort of casual user that did not really need any validation. Once the user had registered, on their dashboard there was an upgrade to organiser button. This then took them to a new form where a couple of extra bits of information are captured. I then also send these users a validation email.

So in my case, it was appropriate to take the user through a two step process. Your users may be like this or totally different. for instance, this process is not appropriate for a system that had students and parents as users.

Whether to have different registration forms really depends how different the information is that you need to collect.

1 like
pdechery's avatar

Hi @Snapey ,

Yes, I understand that. My main question is: is it possible with one single registration page that, after registering, I can redirect users to different views, depending on their polymorphic relationship (that's not even created yet!)

I'll be more specific, so maybe things will become clearer: my project is an online photography contest. So for users I have: candidate, jury and voter. Candidates must have a profile page to submit images as well as their project information. Jury and voters don't have a profile page but will need other specific views/permissions.

As explained above I'm using polymorphic relationships to manage different user types inside a single 'users' table. All users will have to register themselves and after login be redirected to different views.

Since the attachment of the polymorphic relationship must be done at the moment of the user creation, I suppose, does it makes sense to have a single controller method to manage all that?

I'm inclined to use Laravel's built-in auth controller, since it's already there and working, I just don't know if it fits in this type of situation I have.

Many thanks,

Pierre

Please or to participate in this conversation.