Introduction:
I have these Tables with their Models:
- addresses table --> Address model
- users table --> User model
- companies table --> Company model
- properties table --> Property model
Rules:
- each User can have one address only.
- each Company can have one address only.
- each Property can have one address only.
Two possible relationships planning could be done here:
User hasOne Address and Address belongsTo User [i.e. one-to-one relationship] but the disadvantage is that the foreign key here is located in addresses table i.e. user_id field and by repeating the same relationship with other models [Company and Property] we will get another two foreign keys in the addresses table which are [company_id and property_id], then we will end up with 3 foreign keys for 3 Models But only one will be filled by each row record leaving always two foreign keys fields left empty
I feel this is an overhead charge to addresses table.
OR the other way around:
- Address hasOne User and User belongsTo Address [one-to-one relationship also]. This has the advantage that it keeps the foreign key here in the related Model i.e. [User, Company and Property] this way there is no empty fields of foreign keys --- But still this is against the real life Modelling logic which dictates that User hasOne Address more than Address hasOne User.
My Question:
Is there any other relationship that can bind these 4 Models in one relationship similar to Polymorphic Relations but without using MorphMany but instead using MorphOne which strangely enough, I can not find it in the Documentation of Laravel although the method itself exists in Eloquent Relations.
Is this MorphOne polymorphic relation possible and how it is composed?
Existing code:
//Address model
public function user(){
return $this->hasOne(User::class);
}
public function company(){
return $this->hasOne(Company::class);
}
public function property(){
return $this->hasOne(Property::class);
}
//User model
public function address(){
return $this->belongsTo(Address::class);
}
//Company model
public function address(){
return $this->belongsTo(Address::class);
}
//Property model
public function address(){
return $this->belongsTo(Address::class);
}