Venue
有多个Subscription
Subscription
belongsToMany User
如何从User
到Venue
检索所有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)
答案 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