Introduction:
I have these Tables with their Models:
Rules:
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:
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);
}
Here is the MorphOne relationship between Address on one side and User, Company and Property on the other-side:
class Address extends Model
public function addressable(){
return $this->morphTo();
}
}
class User extends Model
public function address(){
return $this->morphOne(Address::class,'addressable'); //note this is not MorphMany()
}
}
class Company extends Model
public function address(){
return $this->morphOne(Address::class,'addressable');
}
}
class Property extends Model
public function address(){
return $this->morphOne(Address::class,'addressable');
}
}
then in the UserController something like that:
$user = User::find(1);
$addressData = [
'street_name' => '5, Golf street',
'country_id' => 100, //etc..
];
//to add new address record related to user:
$user->address()->create($addressData);
dd($user->address);
//to update existing address record related to user:
$user->address->update($addressData);
//dd($user->address);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With