【问题标题】:Laravel 4 model relationship through another modelLaravel 4 模型关系通过另一个模型
【发布时间】:2014-04-02 13:23:53
【问题描述】:

我正在尝试将多个模型绑定到一个表单。目前,我有 4 个模型:

<?php

class DemDataSet extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DEMDataSet';
    protected $primaryKey = 'pk_DEMDataSet';

    public function DemographicReport(){
        return $this->hasOne('DemographicReport','fk_DemDataSet','pk_DemDataSet');
    }

}

class DemographicReport extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DemographicReport';
    protected $primaryKey = 'pk_DemographicReport';

    public function DemDataSet(){
        return $this->belongsTo('DemDataSet','fk_DemDataSet','pk_DemDataSet');
    }

    public function dAgency(){
        return $this->hasOne('dAgency','fk_DemographicReport','pk_DemographicReport');
    }

}

class dAgency extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'dAgency';
    protected $primaryKey = 'pk_dAgency';

    public function DemographicReport(){
        return $this->belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport');
    }

    public function dAgency_10(){
        return $this->hasMany('dAgency_10','fk_dAgency','pk_dAgency');
    }

}

class dAgency_10 extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'dAgency_10';
    protected $primaryKey = 'pk_dAgency_10';

    public function dAgency(){
        return $this->belongsTo('dAgency','fk_dAgency','pk_dAgency');
    }

}

?>

我通过我的控制器将它传递给我的视图,如下所示:

public function index()
{
    //THIS is the line I need help with (I think):
    $agency = dAgency::with('dAgency_10','DemographicReport')->find(1);

    //for troubleshooting:
    echo "<pre>",print_r(dAgency::with('dAgency_10','DemographicReport')->find(1)->toArray()),"</pre>";

    return View::make('test')
        ->with('agency', $agency);
}

我能够绑定除来自 DemDataSet 模型的数据之外的所有内容,我什至不知道如何在它和我的 dAgency 模型之间建立关系。所以基本上,我只希望 DemDataSet 模型中的字段可用于我的视图,那么显然我希望最终能够执行所有 CRUD 操作。

【问题讨论】:

    标签: php model laravel laravel-4


    【解决方案1】:

    查看您的模型,您应该可以通过DemographicReport 访问它。

    即,

    $dataset = $agency->DemographicReport->DemDataSet;
    

    【讨论】:

    • 还是不行。当我在控制器中调用echo "&lt;pre&gt;",print_r($agency-&gt;DemographicReport),"&lt;/pre&gt;"; 时,我得到[relations:protected] =&gt; Array ([DemDataSet] =&gt; ) 指示DemDataSet 为空。但是当我查看该模型 (echo "&lt;pre&gt;",print_r(DemDataSet::find(1)),"&lt;/pre&gt;";) 的结果时,它显然有数据。我仔细检查了我的 hasOne() 和 belongsTo() 是否正确。
    • $agency-&gt;DemographicReport 是否有 fk_DemDataSet 的值?
    • 是的,确实如此。我的模型的关系显然存在问题。我有一个类似的问题,在这里可能是同样的问题:stackoverflow.com/questions/22165781/…。在我真正考虑到这个问题可能是同一个问题之前,我做到了。谢谢你看,我一直在为此自责,我知道这可能很简单。
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 2015-02-12
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2013-06-04
    • 2018-10-21
    相关资源
    最近更新 更多