2016-11-26 118 views
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'); 
      } 
     } 
    } 

回答

0

編輯 - 既然你要求的起始日期日期和結束日期,你只能考慮這明顯的是,對於目前樓市中的最後一項是活動之一。因此,與其去一個HasManyThrough,我相信這將是處理各種情況下可能:)更好的方法

landlords 
id | landlord_name 
---|-------------- 
1 | landmine1 
2 | landmine2 

tenants 
id | tenant_name 
---|-------------- 
1 | haha 
2 | hehe 
3 | hoho 

properties 
id | property_name | 
---|---------------| 
1 | abc   | 
2 | abcd  | 
3 | abcde  | 

landlord_property 
id | property_id | landlord_id | start_date | end_date 
---|-------------|-------------|------------|--------- 
1 | 1   | 1  | SomeDate | SomeDate 
2 | 1   | 2  | SomeDate | SomeDate 
3 | 2   | 1  | SomeDate | SomeDate 

property_tenant 
id | property_id | tenant_id | start_date | end_date 
---|-------------|-------------|------------|--------- 
1 | 1   | 1  | SomeDate | SomeDate 
2 | 2   | 1  | SomeDate | SomeDate 
3 | 1   | 2  | SomeDate | SomeDate 

有在這種情況下,不需要中間/透視表的

class Property extends Model 
{ 
    public function landlords() 
    { 
    return $this->belongsToMany('App\Landlord'); 
    } 

    public function tenants() 
    { 
    return $this->belongsToMany('App\Tenant'); 
    } 
} 

class Tenant extends Model 
{ 
    public function properties() 
    { 
    return $this->belongsToMany('App\Property'); 
    } 

    public function landlord() 
    { 
    return $this->belongsTo('App\Landlord'); 
    } 
} 

class Landlord extends Model 
{ 
    public function properties() 
    { 
    return $this->belongsToMany('App\Property'); 
    } 

    public function tenants() 
    { 
    return $this->hasMany('App\Tenant'); 
    } 
} 

現在你可以很容易地做到

$landlord = Landlord::find(1); 
$propertiesOfLandlord = $landlord->properties; 

$tenantsOfProperty = collect(); 

foreach($propertiesOfLandlord as $property) { 
    $currentTenant = $property->tenants->last(); 
    if($currentTenant) {   
    $tenantsOfProperty->push($currentTenant); 
    } 
} 

$property = Property::find(1); 
$landlordsOfProperty = $property->landlords; 
$tenantsOfProperty = $property->tenants; 

$tenant = Tenant::find(1); 
$propertiesOfTenant = $tenant->properties; 
$landlordOfTenant = $tenant->properties() 
          // ->wherePivot('property_id', 1) If searching for a particular property of tenant 
          ->last() 
          ->landlords 
          ->last(); 

希望這回答了你的問題。

+0

對不起,我應該在我的初始文章中清除它。我需要爲業主和租戶記錄contractStart/contractEnd日期,因此可以生成匯款,所以使用數據透視表是非常重要的。 – MikeF

+0

@MikeF剛剛編輯我的答案......這應該能夠爲你處理各種情況:) – prateekkathal