I have been trying to create a preferences system for my app to control a number of features. There are two different models that can have preferences associated with them, say, Organization and Employee.
I have defined a table called preferences in my schema that looks like this:
preferences: - id: unique id - preference: string - description: string - default: string - type: string (Either 'Employee' or 'Organization', depending on whether this is a user-specific or organization-specific preference)
I have a second table called preference_overrides that just contains the "changed" preference values (if an Employee changes a preference, their provided value is inserted into this overrides table)
preference_overrides: - preference_id: integer references preferences(id) - value: string (user-provided value overriding the default in preferences) - entity_id: integer (this is either an Employee id or an Organization id, depending on the preference being set) - entity_type: string (either 'Employee' or 'Organization', again, depending on the preference type
Needless to say, I'm having trouble getting Eloquent to behave as I like, and I'm not even sure what I'm attempting is correct from a design standpoint. The behavior I'm trying to get is basically an API that can be used like this:
$employee = Employee::find(1) dd($employee->preferences); // Show all preferences of type 'Employee' in a 'preference_title' => 'current value' format $org = Organization::find(1); dd($org->preferences); // Show all preferences of type 'Organization' in a 'preference_title' => 'current value' format
Am I on the right track? Any suggestions?
Thanks in advance!
So it sounds like what you want is a many-to-many polymorphic. Many People can have many preferences. Many Organization can have many preferences. So you need 2 tables.
preferences id name
the "pivot" table
preferenceables preference_id preferenceable_id preferenceable_type | string "Employee" or "Organization
The 2nd part of your question I'm a little confused about but it sounds like you can do that as pivot data inside the preferenceables table
Sign in to participate in this thread!