有很多通过数据透视表

时间:2015-08-03 13:26:02

标签: php laravel laravel-5 eloquent

Venue有多个Subscription

Subscription belongsToMany User

如何从UserVenue检索所有Subscription

我想象的是:

$venue->subscribers // Return all `User`

我的数据库方案:http://www.laravelsd.com/share/AAjuYS

我试过

class Venue {
    public function subscribers() {
        return $this->hasManyThrough('App\User', 'App\Subscription');
    }
}

失败并显示错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.subscription_id' in 'on clause' (SQL: select `users`.*, `subscriptions`.`venue_id` from `users` inner join `subscriptions` on `subscriptions`.`id` = `users`.`subscription_id` where `users`.`deleted_at` is null and `subscriptions`.`venue_id` = 1)

1 个答案:

答案 0 :(得分:-1)

// In Venue class
public function subscribers()
{
    return $this->hasManyThrough('App\User', 'App\Subscription');
}

您可以在文档中阅读相关信息,这非常简单:http://laravel.com/docs/5.1/eloquent-relationships#Has-Many-Through