MySQL user with roles, special fields?

I have an application where a User can be a Supplier or a Store. Now these two roles have very different role fields.

In principle, the Store can place Orders to Suppliers. Thus, the Store can place Orders and the Supplier will receive these Orders.

Where would I put these fields for good practice?

  • Add them all to the User class with null types (this doesn't seem right!)
  • Create a different class for each role. So we have a vendor class with a user_id class and a Shop with a user_id, I will need to query these fields using the $ user-> vendor-> or $ user-> shop-> field ...

None of them are like this path. Any ideas? Thank!

+3


source to share


3 answers


You don't have to stick to one table users

. You can have a vendor model and a store model (with tables suppliers

and shops

).

Then in the file, config/auth.php

you can set up the new auth provider using the Eloquent driver, but with a different model. You can then assign these vendors to any new guards you create.

Read more about authentication and authorization here: https://laravel.com/docs/5.4/authentication

https://laravel.com/docs/5.4/authorization



Edit

After being mentioned in the comments about polymorphic relationships, I think this is better applicable for this type of relationship (multiple user types, each with their own set of fields).

More details here:

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

+3


source


Can the user have multiple stores or short-term deliveries? Your second option seems fine.

User hasMany: supplier, store
- id
- name

Supplier owned, hasOne: user
- id
- user_id
- fields_supplier



Shop belongs, hasOne: user
- id
- user_id
- fields_shop

$user->supplier->fields_supplier;
$user->shop->fields_shop;
$supplier->user->name;
$shop->user->name;


- If you had more information on how you want this to work, it would help.

+1


source


"Supplier" is the role played by "Party" ("Individual" or "Organization")

Your store may receive a Catalog from the Supplier and may request a quote from them.

Your store creates "Purchase Orders" and sends them to the Supplier, and they send you "Order Answers"

The provider is not a user. You could create a "User" for your employees though

Why not just use an existing ERP like Odoo?

+1


source







All Articles