I'm trying to setup a hasManyTrough relationship with Eloquent, but it's unclear how it works from the docs.
Tables:
users
- id
- firstname
- ...etc
accounts
- id
- user_id
- username
- ...etc
roles
- id
- permissions
account_role
- id
- account_id
- role_id
Models
<?php
class User extends Eloquent {
public function account()
{
return $this->hasOne('Account');
}
// This is what I'm trying to achieve
public function roles()
{
return $this->hasManyThrough('Role', 'Account');
}
}
class Role extends Eloquent {
public function accounts()
{
return $this->belongsToMany('Account')->withTimestamps();
}
}
class Account extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
Error and question
The error I'm gettings is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.account_id' in 'on clause'
I know I can do something $user->account->roles
, but I want to be able to do $user->roles
. How do I set this up properly?