2016-11-26 370 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