Two different user classes?

Published 3 years ago by Dean

Just trying to think of how to tackle what I imagine is a fairly common occurrence.

There needs to be two different user types, because there are two different 'account areas'. For example, say we have a directory website, and we have an account area for businesses to create and maintain listings in the directory, and then an account area for customers to interact with the listings. I want to have both separate.

So the way I see it, one option is to just have a property on the User class, to define which type of user this is, but that seems like a poor solution. Another option is to have two different user classes, Customer and Business, with different tables etc., but Laravel is designed in a way that assumes only one User class for Auth purposes, so I imagine I will run into problems. Perhaps, a third option is to have a User class and then have it inherited by a Customer and a Business class, with the User class responsible just for Auth, and then the Customer and Business classes are used for any other model based logic. I'm just not sure how this will work with the Eloquent ORM.

How have others tackled similar problems?

Mort
Mort
3 years ago (3,810 XP)

Could you not use the group/roles solution, tied into a permission system?

Dean
Dean
3 years ago (31,355 XP)

Ahh yes, a fairly simple solution I didn't even think of it. I guess it is similar to the first option I listed (having a property on the user object). I'm guessing this is probably the most common approach.

ThibaudDauce

Yes, I had the same problem with my application.

I made a lot of work to have inheritance in Eloquent ORM on my github (https://github.com/ThibaudDauce/EloquentInheritanceStorage) but it didn't seem good enough for me. I tried to improve Single Table Inheritance pattern (look at it on google)

Today I'm working with two models (Student and Villager inherited from Customer) with one table thanks to my MoloquentInheritance package. Schemas (even if there is no schema in MongoDB) are a bit different for the two models and it works for authentification. You can try my solution if you want, there is no guarantee that it will work but it may give you some ideas.

Please sign in or create an account to participate in this conversation.