【问题标题】:Unable to access relationship Laravel HasManyThrough无法访问关系 Laravel HasManyThrough
【发布时间】: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


【解决方案1】:

我认为你犯了一个错误,因为一般来说,一个房产只有一个房东,但有很多租户。所以房东和财产是hasMany关系。

我建议你删除 LandlordProperty 表并在属性表中创建一个 lanlord_id 外键。现在创建如下公共函数:

在地主模式中:

public function properties()
{
  return $this->hasMany(Property::class)
}

在属性模型中:

public function landlord()
{
  return $this->belongsTo(Landlord::class)
}

现在尝试查询。

【讨论】:

  • 问题是,有时房产被卖掉了,新的房东被任命了。如果是这种情况,我需要用于汇款的contractStart 和contractEnd 日期。
  • @mike 我可以理解联系日期绑定到属性或租户表。因此,您首先获取合同详细信息,然后使用新的房东 ID 和新日期更新表格。
  • @MikeF 在外键末尾创建外用 {code} ->onUpdate('cascade') 时更新地主id,以便它可以允许您更新外键。跨度>
【解决方案2】:

我已经设法通过使用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');
    }
}

这也解决了中间控制器的问题,因为我使用的是数据透视表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 2018-10-18
    • 2023-03-09
    • 2018-07-24
    • 2015-10-13
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多