I have a multi-subdomain CRM. Each company gets their own subdomain.
There are companies and users. Addresses are a separate table and can be attached to either a user or a company by a pivot table.
This code returns all of the addresses in within the Subdomain::class model:
public function getAddresses() {
return Address::wherehas('users', function($q) {
$q->where('users.subdomain_id', $this->id);
})->orWherehas('companies', function($q) {
$q->where('companies.subdomain_id', $this->id);
})->get();
}
I would like to convert this to a relation instead, something like:
public function addresses() {
return $this->morphToMany(...);
}
Is morphToMany the correct method? How can I attach both companies and users tables to get all addresses that have a relation with the subdomain_id?
table schema:
addresses
- id
- address
address_pivot
- addressable_id
- addressable_type ('User' or 'Company')
- address_id
companies
- id
- company_name
users
- id
- name