Laravel 4模型关系通过另一个模型

时间:2014-03-01 11:37:22

标签: php model laravel laravel-4

我正在尝试将多个模型绑定到表单。目前,我有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操作。

1 个答案:

答案 0 :(得分:0)

查看您的模型,您应该只能通过DemographicReport

访问它

即,

$dataset = $agency->DemographicReport->DemDataSet;
相关问题