2016-11-26 5 views
0

現在、モデルのDBクエリをハードコーディングして、テーブル関係を処理しています。代わりにLaravelのEloquent関係を利用したいと思います。ラーベル関係HasOneThrough

私は3つのモデル(プロパティ、賃貸住宅、テナント)を持っています。これらはすべて、ある方法で互いに結合されています。私は関係IDを保持する2つの中間テーブル(TenantProperty、LandlordProperty)を持っています。

ピボットテーブルには重要なcontractStart/contractEndデータが格納されています。このデータは、将来の送金のために不可欠です。

Propertyテーブル:

------------------------ 
|  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 

私の質問です。 hasManyThroughではなくhasOneThroughを使用できますか?

私のモデルの一例:

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

編集 - あなたはSTART_DATEとEND_DATEを必要とするので、あなたはそれだけで、現在のプロパティの最後のエントリがアクティブ一つであることを、この明らか検討することができます。そのため、代わりに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