現在、モデルの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');
}
}
}
申し訳ありませんが、私は最初の投稿で削除する必要がありました。家主やテナントのcontractStart/contractEndの日付を記録する必要があるので、送金を生成できるので、ピボットテーブルを使用することが不可欠です。 – MikeF
@MikeFちょうど私の答えを編集...これはあなたのためのすべての種類のケースを処理することができるはずです:) – prateekkathal