模型之间的关系问题

时间:2014-02-25 01:25:45

标签: php laravel laravel-4 eloquent

我无法让我的模特之间的关系发挥作用。

IRL中有这种情况: 公司每年在年度股东大会上发布一些所有权信息 年度股东大会的说明包含有关公司人员所有权的信息

我已经制作了这些模型以及它们之间的关系:

class Company extends Eloquent{
    public function annualmeetings()
    {
        return $this->hasMany('AnnualMeeting');
    }
}

class AnnualMeeting extends Eloquent{
    public function company()
    {
        return $this->belongsTo('Company');
    }
    public function ownerships()
    {
        return $this->hasMany('Ownership');
    }
}

class Ownership extends Eloquent{
    public function annualmeeting()
    {
       return $this->belongsTo('AnnualMeeting');
    }
    public function person()
    {
        return $this->belongsTo('Person');
    }
}

class Person extends Eloquent{
    public function ownerships()
    {
        return $this->hasMany('Ownership');
    }
}

现在我想要取消一个人拥有所有权的所有公司以及公司类中的一些信息来自所有权类的一些信息以及AnnualMeeting类中的变量

我尝试过这样的事情:    $ companies = Person :: with('ownerships.annualmeeting.company')

但是我没有得到相关模型的数据。我做错了什么?

< 3

更多信息: 我试过这个:

foreach ($person->ownerships as $ownership)
    echo $ownership->annualmeeting->date

我收到错误“试图获取非对象的属性”

而且:

$person = Person::with('ownerships.annualmeeting')->findOrFail($id);
echo '<pre>';
print_r($person);
echo '</pre>';

给出(在结果中):

[relations:protected] => Array
                      (
                       [annualmeeting] => 
                       )

更容易理解吗? :)

0 个答案:

没有答案
相关问题