1

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!


Desired output

3
  • A bit confused with what you want displaying? One user with a list of all of it's companies in one cell OR For every company the user is duplicated? Commented Oct 30, 2015 at 13:40
  • And I can definitely confirm that un-normalising your database would be a dumb move ;-) Commented Oct 30, 2015 at 13:40
  • Thanks for the response David! My end goal is to have a table that lists out Users and Companies together. I'm not worried about displaying the relationship between users and companies, but instead treat them similar for display. Commented Oct 30, 2015 at 13:52

1 Answer 1

0

Like said in the https://larachat.slack.com . use a SQL UNION and / or an SQL VIEW to bring them together and have one queryable view.

http://www.w3schools.com/sql/sql_union.asp and / or http://www.w3schools.com/sql/sql_view.asp

The main credits go to Joseph Silber.

$networkable = DB::table('users')->union(DB::table('companies'))->paginate(10);
1
  • Thank you for taking the time to follow up here! Commented Oct 30, 2015 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.