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

Belhedi's avatar

Multiple users with same login page

Hi guys I want to put all my different users (teachers, students, admins) in the same table but how can I differentiate between them when I add the relationships in this table. For example : the teachers belongs to a departement table, and the students belongs to another table which is faculty table ! The problem is when I add those relationships to the users table it will add automatically to all types of users (teachers, students, admins...).

0 likes
3 replies
Web Confection's avatar
public function teachers()
{
        return $this->hasMany(User::class)->where('teacher',1);
}

or similar.

Belhedi's avatar

@CJJ - thnx for replying. But what do yo mean by adding : ->where('teacher', 1) ??

phpMick's avatar

I think he is saying that should should add a column to your users table (in the example it is called teacher and populated with the value 1 if they are a teacher).

The relationship will then only return users who have a 1 in this column.

Might be better if you called the column 'type' then used 1, 2, 3 (obviously using constants ) for the teachers, students and admins?

Seems like a neat solution to me.

Mick

1 like

Please or to participate in this conversation.