Wondering if I could get your thoughts/experience on the following situation? Please let me know if I can clarify any of the information below.
Problem
I need to interact with Users and Companies, retrieve, filter/sort, and paginate into an integrated html table.
Relationship structure: user
hasMany()
company
and company
belongsTo()
user
.
Clarification: Only a user
can log in. A company
can not log in.
Potential Solutions (as I can see)
Polymorphism
Using a networkable
model/table, relate the users
and companies
tables through a morphTo()
relationship called "displayable()". Retrieval of the merged tables would be done through Networkable::with('displayable')->get()
.
Pluses
1) Database maintains clean separation of users
and companies
.
Trade-offs
1) Requires maintaining the relationship every time a user
or company
is created/deleted
2) Complicates typical eloquent functions since user
and company
details would be retrieved via relationship or distant relationship
Single Table
Store companies
in the users
table and treat them as a different entity, even though they are stored in the same table.
Pluses
1) Filtering would be pretty natural, and likely faster, since we would be dealing with a single table, and minimal complicated joins.
Trade-offs
1) Breaking Second Normal Form by adding extra columns in the users
table, usable only to the "company" type of user.
2) Not all records in the users
table would actually be users. Confusing? Yes. Certain records in the users
table would be companies
, which would have different column information than users
. companies
would likely have a boolean called is_company
or something similar to differentiate between a company
record and a user
record.
3) Would require a relationship joining a user
to the users
table itself. That way a user could "own" a company
.
4) Would need to restrict logging in as a company
since that isn't the intent of a company
.
What do you think? Is one of these options better than the other? Am I missing another solution? Thank you!