Laravel关系HasOneThrough [英] Laravel Relationships HasOneThrough
问题描述
当前,我正在对模型中的数据库查询进行硬编码以处理表关系.我想利用Laravel的口才来代替.
Currently, I'm hardcoding DB queries in my Models to handle table relationships. I'd like to take advantage of Laravel's Eloquent relationships instead.
我有3个模型(属性,房东,租户),它们以一种或另一种方式彼此结合.我有2个中间表(TenantProperty,LandlordProperty),其中包含关系ID.
I have 3 Models (Property, Landlord, Tenant), which are all joined to each other in one way or another. I have 2 intermediate tables (TenantProperty, LandlordProperty) which holds relationship id's.
数据透视表保存着重要的contractStart/contractEnd数据,这对于将来的汇款产生至关重要.
The pivot tables hold important contractStart/contractEnd data, which is vital for future remittances to be generated.
属性表:
------------------------
| id| propTitle|
------------------------
| 1| Property 1|
------------------------
| 2| Property 2|
地主表:
------------------------
| id| firstName|
------------------------
| 1| Bob|
------------------------
| 2| Roger|
租户表:
------------------------
| id| firstName|
------------------------
| 1| Ted|
------------------------
| 2| Peter|
TenantProperty表:
TenantProperty table:
-----------------------------------------------------------------
| 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表:
LandlordProperty table:
-----------------------------------------------------------------
| 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
吗?
My question is; Is it possible to have a hasOneThrough
as opposed to 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');
}
}
}
推荐答案
已编辑-由于您需要start_date和end_date,因此您只能认为这很明显,该当前属性的最后一个条目是活动条目.因此,我相信这不是处理HasManyThrough的好方法,而是处理所有可能情况的更好方法:)
Edited - Since you require the start_date and end_date, you can only consider this obvious that the last entry for that current property is the active one. Therefore, instead of going for a HasManyThrough, I am sure this will be a better approach for handling all kinds of cases possible :)
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
在这种情况下,不需要中间/数据透视表
There is no need of intermediate/pivot table in this case
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();
希望这能回答您的问题.
Hope this answers your question.
这篇关于Laravel关系HasOneThrough的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!