So basically I have 3 different types of users, all in their own table, however Laravel only allows me to use one table for authentication so I was thinking of making a link table in the database to hold the usernames and passwords from all the tables and use this table for logging users in.
My understanding of link tables is that they can only take the primary key from other tables so therefore I would only be able to get the usernames from each of my 3 tables into my link table, and not the passwords.
Is there a way that I can bring both the username and password fields into my link table? (& if possible the email fields too?, if not username and password is fine).
Thanks, Sam.
You can still have a users
table and have separate tables for holding the other information, so you could do something like:
users(id, name, email, password, user_type_id)
user_types(id, name)
business_sellers(user_id, business_seller_columns...)
individual_sellers(user_id, individual_sellers_columns...)
admin(user_id, admin_columns...)
Then you can log your user in using the users
table and then pull the relevant information from the other tables depending on the user_type
Or you could use polymorphic relationships and do:
users(id, name, email, password, role_id, role_type)
business_sellers(id, business_seller_columns...)
individual_sellers(id, individual_sellers_columns...)
admin(user_id, admin_columns...)
Then you models would look like:
class User extends Model
{
/**
* Get all of the owning role models.
*/
public function role()
{
return $this->morphTo();
}
}
class Admin extends Model
{
/**
* Get all of admins users.
*/
public function users()
{
return $this->morphMany('App\User', 'role');
}
}
class BusinessSeller extends Model
{
/**
* Get all of business_sellers users.
*/
public function users()
{
return $this->morphMany('App\User', 'role');
}
}
class IndividualSeller extends Model
{
/**
* Get all of individual_sellers users.
*/
public function users()
{
return $this->morphMany('App\User', 'role');
}
}
The you simply need to get your user and call role and the relevant model will be returned:
$user = App\User::find(1);
dd($user->role);