Sunday, March 19, 2017

Laravel - different models using the same base table

I'm trying to implement a system with multiple account types. I've defined 3 new account types by creating their associated guards and models, however I'm slightly stumped on how to configure Laravel to work with the database structure that I want.

My issues are as follows:

  • On attempting a password reset, the default broker is used. Rather than attempting each of the brokers, it would be better if a record could be obtained from the database indicating which type of account is being dealt with.

  • Accounts email addresses will need to be cross-table unique, so a user of one account type cannot have the same email as a user of another account type.

The solution I have come up with to this is having a single "base table" which stored information that all account types will have, such as an email address and some form of unique account ID. Additionally, there will be a field in that table indicating what type of account the user has, giving me an ideal way to handle the password reset situation I described and also meaning that I don't have to check other tables for uniqueness before doing anything, I only need to check the base table.

The problem I am having is that I don't know how to set up the models to do this, as it seems to me models are designed to work with a single table. It makes sense to me that the specific information table about an account type will have to have its own form of unique ID as well as have a field for base table UIDs, so that any operations which get results from the database can join the tables with the base table's UID.

My question is as follows: is there a better way to design the account type tables, and if not how should I go about achieving what I've described?



via VortixDev

Advertisement