2

I want to ask how to make two types of users in laravel. I have two tables, one for the customer and one for the client and my question is how to make that difference. Do I have to make two different models or to use model User and make some functions in middleware?

Thank you.

2
  • 1
    My question in the first place is why do you use two tables for the users? What is the difference between the users? Is not the generic information about the user the same? And would it not be better to have that in a separate table referring to the user. If you are thinking about using a user role system use instead an enum or an seperate table for this. Commented Nov 23, 2016 at 15:09
  • 1
    I have two different tables because of the informations about that users, client and customer have different informations and thats why I'm doing that Commented Nov 23, 2016 at 17:31

2 Answers 2

2

If you're looking for the simpliest solution, you can add role column to users table. Then you can check if user is a client or a customer globally with:

if (auth()->user()->role === 1) {
    // It's a client.
}

You can add some helper methods to check if user is a client or a customer:

public function isClient()
{
    return auth()->user() && auth()->user()->role === 1;
}

To open the part of the website for client only you should use route groups and middleware.

Sign up to request clarification or add additional context in comments.

3 Comments

It's not about the roles of users, I have different informations about client and customer and thats why I can't put client and customer at the same table..
@epowah use the method I described and add two models which are in one-to-one relationship with User model. Store same data (email, password, name etc.) in User model and everything else in ClientProfile and CustomerProfile, for example. If your app has a lot of relationships and complicated queries, I'd recommend you to store all users related data in a single users table.
can you give me some more details or if you can some example I will be very glad
0

I've run into the same situation and I've resolved it by using a package that handles multi-authentication: Check out this package:

There are also a blog post about this situation:

and of course more StackOverflow questions:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.