Laravel – Eloquent Joins

Solution:

In User model:

public function accounts()
{
    return $this->hasMany('Account');
}

In Account model:

public function user()
{
    return $this->belongsTo('User');
}

public function accountType()
{
    return $this->belongsTo('AccountType', 'accounttype_id', 'id');
}

In AccountType model:

public function accounts()
{
    return $this->hasMany('Account', 'accounttype_id', 'id');
}

Then in your Controller:

// This will return a user with all accounts ($user->accounts will be collection)
$user = User::with('accounts')->find(Auth::user()->id);

Or:

$user = User::with('accounts.accountType')->find(Auth::user()->id);

// You may pass the $user as well and then loop the $user->accounts in view
return View::make('accounts.accounts')->with('accounts', $user->accounts);

In your view you may loop to get all accounts like:

@foreach($accounts as $account)
    {{ $account->name }}
    {{ $account->accountType->name }}
@endforeach

Since $user->accounts is a collection so you may either run a loop or specifically get an account using something like this:

{{ $accounts->first()->name }}; // Get first one's name
{{ $accounts->get(0)->name }}; // Get first one's name
{{ $accounts->get(1)->name }}; // Get second one's name
{{ $accounts->last()->name }}; // Get last one's name

if you pass the $user instead of $accounts like this:

return View::make('accounts.accounts')->with('user', $user);

Then change the loop as well, like this:

@foreach($user->accounts as $account)
    {{ $account->name }}
    {{ $account->accountType->name }}
@endforeach

You can make sure if that user has accounts in the view before you start the loop, like:

@if($user->accounts->count())
    // Loop Here
@endif