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