【发布时间】:2016-11-29 21:41:57
【问题描述】:
我有 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 的数据。
- 房产#1 -> 房东房产#1 -> 房东#1
- 属性 #2 -> 房东Property #2 -> 房东 #2
- 财产 #11 -> 房东财产 #3 -> 房东 #3
【问题讨论】:
标签: php laravel laravel-5 model