如何在laravel-4中创建三个模型之间的关系


How to create a relationship across three models in laravel-4?

我有以下数据结构:

 Table t:
      (PK)id
          order_id (this links to table or's id)

 Table is:
      (PK)id
          tracking_id (this links to table or's tracking_id)
 Table or:
      (PK)id    (this links to table t's order_id)
      (FK)tracking_id

现在在我的模型(T.php)中

我设置了以下关系:

 public function iS()
 {
    return $this->hasMany('is', 'tracking_id', 'order_id');
 }

现在显然这不起作用,因为tracking_id与order_id不匹配。

如何将这三个表链接在一起,以便检索与t表对应的is数据?

我在我的控制器里这样称呼它:

  $o = O::with('t', 't.iS')->where('id','=',$id)->first(); (O is my model for "o" table)

您正在寻找hasManyThrough关系。http://laravel.com/docs/eloquent#has-许多通过

在文档示例中:Country具有UsersUsers具有Posts。要获得你定义的每个国家的职位:

class Country extends Eloquent {
    public function posts()
    {
        return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
    }
}

在您的示例中:TOrOrIs