I've got an Accounts
table and a People
table in my database and I'm trying to set up models with accurate Laravel eloquent relationships.
The tables are linked via a contact_id
column.
The People table contains employees as well as customers- the only difference is the value of the contact_id_type column in the Accounts table, which can be either "Customer" or "Employee".
Accounts
+------------------------------------------------+
| id | account | contact_id | contact_it_type |
+-----------------------------------------------+
| 1 | XYZ-67890 | 9876543210 | Employee |
| 2 | XYZ-56781 | 9876543211 | Customer |
| 3 | XYZ-56792 | 9876543212 | Employee |
| 4 | XYZ-46793 | 9876543213 | Customer |
| 5 | XYZ-45894 | 9876543214 | Employee |
+-----------------------------------------------+
People
+--------------------------------------------------------+
| id | fname | lname | email |
+--------------------------------------------------------+
| 9876543210 | John | Doe | john.doe@example.com |
| 9876543211 | Jane | Doe | jane.doe@example.com |
| 9876543212 | Peter| Parker | peter.parker@example.com |
| 9876543213 | Mary | Jane | mary.jane@example.com |
| 9876543214 | Bruce| Wayne | bruce.wayne@example.com |
+--------------------------------------------------------+
I've made the following Account model. How can I incorporate the contact_id_type so that the relation is correct?
class Account extends Model
{
public function employee(): BelongsTo
{
return $this->BelongsTo(Person::class, 'contact_id')
}
public function customer(): BelongsTo
{
return $this->BelongsTo(Person::class, 'contact_id')
}
}
source https://stackoverflow.com/questions/77455100/how-can-i-create-two-eloquent-belongsto-relationships-on-the-same-two-models
No comments:
Post a Comment