在用户 I.E. 的前两个参数之间获取具有主元 table 的外键列Auth::user()->账户->工作

Get foreign key column with a pivot table in between the first two arguments of a user I.E. Auth::user()->account->job

好的,所以我一直在谷歌搜索,但一无所获。 我如何获取 Auth::user() 对象,然后使用一个数据透视表获取该用户的所有帐户,然后获取一个子项目,比如工作。 所以。

//gets me the user object
$user = Auth::user();
//pivot table  account_user
//gets me all the users accounts
$accounts = $user->account()->get();
//this gets me all the accounts with the correct jobs
$accounts =  Account::with('job')->get();

class Account extends Model{
    public function user() {return $this->belongsToMany('App\User')->withPivot('a','b','c','d')->withTimestamps();} 
    public function job(){return $this->belongsTo('App\Job');}
}
class User extends Model implements AuthenticatableContract,AuthorizableContract,CanResetPasswordContract{
 public function account(){return $this->belongsToMany('App\Account')->withPivot('a','b','c','d')->withTimestamps();
}

所以我想要用户拥有的所有帐户,并且在每个帐户上我想要 job_id 它们已替换为外部列名称或能够从 json 获取它它制造的对象。

为此,您需要像这样在帐户模型上使用 with。 $accounts = $user->account()->with('Financialinstitution')->get();