I have 5 models, 2 are pivot/intermediary models.
Tenant
,Landlord
,Property
,LandlordProperty
,TenantProperty
Landlord & Tenant need to be accessible both from within the Property model and also exclusively.
Currently, I am unable to access the Landlord model from Property, it simply returns no data.
Tables:
Property table:
------------------------
| id| propTitle|
------------------------
| 1| Property 1|
------------------------
| 2| Property 2|
Landlord table:
------------------------
| id| firstName|
------------------------
| 1| Bob|
------------------------
| 2| Roger|
Tenant table:
------------------------
| id| firstName|
------------------------
| 1| Ted|
------------------------
| 2| Peter|
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 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
Models:
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');
}
}
The following loop returns no data on $property->landlords
and I am unsure why
@foreach ($properties as $property)
<tr class="clickable-row" data-href="/property/view/">
<td></td>
<td></td>
<td></td>
<td> </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></td>
</tr>
@endforeach
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire