In order to pull relationships on a pivot table, you must create a custom pivot model.
The misunderstanding I was having was that the pivot property was already pulling a pivot model I created, whereas it turns out that in Eloquent, you create what is known as a CustomPivotModel.
In my App\Models namespace I have models for Account, User, and Permission objects.
In my App\Models\Pivot namespace, I created an AccountUserPermission class that extends Illuminate\Database\Eloquent\Relations\Pivot.
namespace App\Models\Pivots;
use Illuminate\Database\Eloquent\Relations\Pivot;
// Take note, we must extend the Pivot type!
class AccountUserPermission extends Pivot
{
public function account()
{
return $this->belongsTo('App\Models\Account');
}
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id', 'user_id');
}
public function permission()
{
return $this->belongsTo('App\Models\Permission');
}
}
This is what I added to my User model in App\Models\User:
// You will of course have to add a use statement for
// the types we will be referencing
use App\Models\Account;
use App\Models\Pivot\AccountUserPermission;
use Illuminate\Database\Eloquent\Relations\Pivot;
// Override the newPivot function on the User model
public function newPivot(Model $parent, array $attributes, $table, $exists, $using = null)
{
if ($parent instanceof Account) {
return new AccountUserPermission($parent, $attributes, $table, $exists, $using);
}
return parent::newPivot($parent, $attributes, $table, $exists, $using);
}
The code on the other objects looks similar, we simply just override the newPivot function on models that will be using the custom pivot model.
// Account model (I'll shorten it up this time, it should practically
// look identical to the code above with the user model)
public function newPivot(Model $parent, array $attributes, $table, $exists, $using = null)
{
if ($parent instanceof User) {
return new AccountUserPermission($parent, $attributes, $table, $exists, $using);
}
return parent::newPivot($parent, $attributes, $table, $exists, $using);
}
The solution was fairly easy. Now when I need to access that pivot table, if I have a user object, it looks like this:
// Pull an arbitrary user for this example
$user = App\Models\User::first();
// When we access the pivot property, it is calling our
// overridden newPivot method and returning an instance
// of AccountUserPermissions.
// Check if this user is an owner
if ($user->pivot->permission->owner)
{
dd("Congratulations, you've won!");
}