User with multiple companies (different type)

Tags: , , , ,



I built an application in Laravel that is a simple CRUD app. Now im struggling with migrations a bit.

User belongs to Company. Company can have many users.

Company type can be Supplier, Customer or Internal. All 3 of company types have different fields. For example

Supplier table contains: company_name, incoterm, payment_term, business type…etc

Customer table contains: company_name, artwork, discount, target_margin… etc

Internal table contains only company_name, updated_at and created_at fields.

So all of thame have company_name field, all other fields are different.

When user is logged in i want to be able to recognize is he supplier/customer or internal. I want to be able to say Supplier::find($id)->name which should return me name of company. Supplier::find($id)->users should return all users that supplier have. Same for customer, same for internal.

i’m not sure should i have user_type column in users table that will declare is user supplier/customer on internal or what should i do?

I was thinking about something like this :

Tables

User:

id
name
last_name
company_type
company_id

Supplier:

id
company_name
incoterm
payment_term
business type

Customer:

company_name 
artwork
discount 
target_margin

Internal:

company_name

Example of user table:

-
id: 1
name: "Aleksandar"
last_name: "Alex"
email: "alex@admin.com"
password: "$2y$10$4cYfe/J71VoCib.esHasWud7ueRHURMmcaqSECBJjNJ.y05qtXYXO"
company_type: "\App\Model\Supplier"
company_id: 1
-
id: 2
name: "Another"
last_name: "User"
email: "user@admin.com"
password: "$2y$10$4cYfe/J71VoCib.esHasWud7ueRHURMmcaqSECBJjNJ.y05qtXYXO"
company_type: "\App\Model\Customer"
company_id: 1
-
id: 3
name: "Internal"
last_name: "User"
email: "user@admin.com"
password: "$2y$10$4cYfe/J71VoCib.esHasWud7ueRHURMmcaqSECBJjNJ.y05qtXYXO"
company_type: "\App\Model\Internal"
company_id: 1

After i have this i can do something like user.company_type::find($id) and i will get everything from Supplier/Customer or Internal with given $id.

Is this right way to do it ? If yes then how can i get all user from supplier N? Supplier::find($id)->users or in future Supplier::find($id)->posts will require additional eloquent filter because company_id in users can be 1 for Supplier,Customer or Internal as on example from above.

Edit: I’m using Laravel v6.18.35

Answer

As @hsibboni mentioned probably what you need is polymorphic relations.

So you will need something like this in your models:

User

public function company()
{
    return $this->morphTo('company', 'company_type', 'company_id', 'id');
}

Supplier, Customer and Internal

public function users()
{
    return $this->morphMany(User::class, 'company', 'company_type', 'company_id', 'id');
}

And when you load relation company on User you will get the company of that user, also if you load relation users for Supplier, Customer, or Internal you will get Users that belong to them.

I think this is what you need.

Good luck!



Source: stackoverflow