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

sarmadindhar's avatar

Help in eloquent Relations

I have 3 types of users which can login in system, I have created separated tables for them b/c they have different fields/data from each other.I have passed user_id which is primary key in users table as foreign key in customers and company table

users(id,email,password,user_type)
customers(id,user_id,first_name,last_name,etc,etc)
company(id,user_id,company_name,address,etc)

I have two other tables

services(id,name)
company_service(id,company_id,service_id)

The problem is that How i should make eloquent relationships among these tables as I can get the information like user profile from related table or company's services etc.

class User extends Authenticatable
 public function customer(){
        return $this->hasOne('\App\Customer');
    }
    public function company(){
        return $this->hasOne('\App\Company');
    }
}

class company extends Model{
    return $this->hasMany('App\Service')
}
0 likes
7 replies
mo2l's avatar

Actually I don't see the need for a global scope in that case. But I might miss a detail on this one .

My initial approach to solve your issue would have been polymorphic relationships:

https://laravel.com/docs/5.7/eloquent-relationships#polymorphic-relationships

This would probably need some restructuring of your tables like the following:

Let's assume we will call the polymorphic relationship information.

users(id,email,password,information_type, information_id)
customers(id,first_name,last_name,etc,etc)
company(id,company_name,address,etc)

you could keep the user_id fields on customers and company if you need it.

You would need to update your user, customer and company model to add the specific relationship.

in this case you could always do $user->information to gather the information of any user type.

This solution would easily be extendable without the need to change the user model to add a new relationship.

Hope I could help you :)

D9705996's avatar

@MO2L - I would avoid polymorphic relations if you can. One of the issues is that you lose referential integrity at a database level (e.g. foreign keys between you tables) and it's also quite hard to rationalise about else silly coming from a traditional RDMS background. The link I provided allows you to keep the basic user structure but sub class your users

(Polymorphic relationships are awesome when used correctly)

mo2l's avatar

@D9705996 - Thank you for the perspective from an RDMS point of view. :)

To be honest I did not had that in mind. do you have any insight on when a case where you would use polymorphic relationships? (I am just curious right now)

D9705996's avatar

@MO2L - I tend to use polymorphic relations when the sub classes are very different and can't be achieved with a type column. E.g. user/admin/customer could be done with a user_type column.

In mymonitoring app I support lots of different monitoring types, SNMP, SSH, etc and each of these are very different in their make up an have different attributes so makes sense to have separate tables with polymorphisms

But it's the bit of my app I always struggle to debug... the convience doesn't always pay off

mo2l's avatar

@D9705996 thank you for letting me have an insight of your way of doing it :)

D9705996's avatar

@MO2L - Just remember there isn't a right way to do anything... just do what you are happy with. Only guided guidelines I can give is keep it simple stupid (KISS).

When I first started I used polymorphic relations everywhere and I now look at these projects with WTF. I could have used the link I provided and kept things easy but polymorphic was what the cool kids were using.

The best advice I can give is before writing your code, have a look at the laravel framework, and how it does the same concept and borrow. E.g. if you write a driver based solution look at the cache driver.

Please or to participate in this conversation.