[英]Unable to access relationship Laravel HasManyThrough
我有5个模型,其中2个是数据透视/中间模型。
Tenant
, Landlord
, Property
, LandlordProperty
, TenantProperty
既需要从“房地产”模型中访问房东,也可以仅从房东和租户那里访问。
目前,我无法从Property访问Landlord模型,它只是不返回任何数据。
表格:
属性表:
------------------------
| 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
房东属性表:
-----------------------------------------------------------------
| 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
楷模:
class Landlord extends TenantModel {
public function properties(){
return $this->hasManyThrough('App\Property', 'App\LandlordProperty',
'property_id', 'id', 'id');
}
}
class Tenant extends TenantModel {
public function properties()
{
return $this->hasManyThrough(
'App\Property', 'App\TenantProperty',
'property_id', 'id', 'id');
}
}
class Property extends TenantModel {
public function landlords()
{
return $this->hasManyThrough(
'App\Landlord', 'App\LandlordProperty',
'landlord_id', 'id', 'landlord_id');
}
public function getLandlordAttribute()
{
return $this->landlords->first();
}
public function tenant()
{
return $this->hasManyThrough(
'App\Tenant', 'App\TenantProperty',
'tenant_id', 'id', 'property_id');
}
}
class TenantProperty extends TenantModel {
public function tenant() {
return $this->belongsTo('App\Tenant');
}
public function property(){
return $this->belongsTo('App\Property');
}
}
class LandlordProperty extends TenantModel {
public function property(){
return $this->hasOne('App\Property');
}
public function landlord(){
return $this->hasOne('App\Landlord');
}
}
以下循环不返回有关$property->landlords
数据,我不确定为什么
@foreach ($properties as $property)
<tr class="clickable-row" data-href="/property/view/{{ $property->id }}">
<td>{{ $property->id }}</td>
<td>{{ $property->streetAddress }}</td>
<td>{{ $property->type }}</td>
<td>{{ $property->firstName }} {{ $property->lastName }}</td>
@php
echo '<pre>' . var_export($property->landlords, true) . '</pre>';
@endphp
<td>
@php
if($property->status == 1){
echo "<b style='color: green;'>Active</b>";
}else{
echo "<b style='color: red;'>Disabled</b>";
}
@endphp
</td>
<td>{{ date('d M Y', strtotime($property->created_at)) }}</td>
</tr>
@endforeach
*编辑1 *
好的,因此将App\\Property landlords() function
为:
public function landlords()
{
return $this->hasManyThrough(
'App\Landlord', 'App\LandlordProperty',
'property_id', 'id', 'id');
}
现在开始返回一些数据,但是不一致。
它正在遍历LandlordProperty
表中的ID,而不是提取相关属性ID的数据。
我认为您会犯错,因为一般来说,一个物业只有一个房东,却有许多租户。 所以房东和财产有很多关系。
我建议您删除LandlordProperty表并在属性表中创建lanlord_id外键。 现在创建如下的公共职能:
在lanlord模型中:
public function properties()
{
return $this->hasMany(Property::class)
}
在属性模型中:
public function landlord()
{
return $this->belongsTo(Landlord::class)
}
现在尝试查询。
我已经设法通过使用belongsToMany
解决此问题,如下所示:
class Tenant extends TenantModel
{
public function property()
{
return $this->belongsToMany('App\Property', 'tenant_property', 'property_id', 'tenant_id')->withPivot('isActive','contractStart','contractEnd', 'property_id');
}
}
class Property extends TenantModel
{
public function landlord()
{
return $this->belongsToMany('App\Landlord', 'landlord_property', 'property_id', 'landlord_id')->withPivot('isActive','date_from','date_to', 'property_id');
}
public function tenant()
{
return $this->belongsToMany('App\Tenant', 'tenant_property', 'property_id', 'tenant_id')->withPivot('isActive','contractStart','contractEnd', 'property_id','vacatedDate');
}
}
class Landlord extends TenantlModel
{
public function property(){
return $this->belongsToMany('App\Property', 'landlord_property', 'landlord_id', 'property_id')->withPivot('isActive','date_from','date_to', 'property_id');
}
}
这也摆脱了使用中间控制器的问题,因为我正在使用数据透视表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.