0
目前,我在我的模型中对数据库查询进行硬编码以处理表关系。我想要利用Laravel的雄辩关系。Laravel关系HasOneThrough
我有3个模型(物业,房东,租客),这些模型都以某种方式相互联系在一起。我有2个中间表(TenantProperty,LandlordProperty),它拥有关系ID。
数据透视表包含重要的contractStart/contractEnd数据,这对于将来生成的汇款至关重要。
属性表:
------------------------
| id| propTitle|
------------------------
| 1| Property 1|
------------------------
| 2| Property 2|
楼主表:
------------------------
| id| firstName|
------------------------
| 1| Bob|
------------------------
| 2| Roger|
租户表:
------------------------
| id| firstName|
------------------------
| 1| Ted|
------------------------
| 2| Peter|
TenantProperty表:
-----------------------------------------------------------------
| id| tenant_id| property_id|contractStart| contractEnd
-----------------------------------------------------------------
| 1| 1| 2| 01-01-1970| 01-01-1971
-----------------------------------------------------------------
| 2| 2| 1| 01-01-1970| 01-01-1971
LandlordProperty表:
-----------------------------------------------------------------
| id| landlord_id| property_id|contractStart| contractEnd
-----------------------------------------------------------------
| 1| 1| 1| 01-01-1970| 01-01-1970
-----------------------------------------------------------------
| 2| 2| 2| 01-01-1973| 01-01-1973
我的问题是;是否有可能有hasOneThrough
而不是hasManyThrough
?
我的模型的例子:
class Tenant extends TenantModel
{
public function tenantProperty() {
return $this->hasOne('App\TenantProperty');
}
}
class Property extends TenantModel
{
public function tenant()
{
return $this->hasOne('App\TenantProperty');
}
}
class Landlord extends TenantModel
{
public function properties(){
return $this->hasMany('App\LandlordProperty');
}
}
class LandlordProperty extends TenantModel
{
public function property(){
return $this->hasMany('App\Property');
}
public function landlord(){
return $this->hasOne('App\Landlord');
}
}
class TenantProperty extends TenantModel
{
public function tenant() {
return $this->belongsTo('App\Tenant');
}
public function property(){
public function product() {
return $this->belongsTo('App\Property');
}
}
}
对不起,我应该在我的初始文章中清除它。我需要为业主和租户记录contractStart/contractEnd日期,因此可以生成汇款,所以使用数据透视表是非常重要的。 – MikeF
@MikeF刚刚编辑我的答案......这应该能够为你处理各种情况:) – prateekkathal