Using the withPivot method whenever defining the relation, you can get the ID only
// User
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class)->withPivot('assigned_by');
}
However, you probably want a User instance to represent the assignor? In that case, you will need an intermediate pivot model into which you can define an assignor belongs to relation:
// User
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class)
->using(RoleUser::class)
->withPivot('assigned_by');
}
// RoleUser
public function assignor(): BelongsTo
{
return $this->belongsTo(User::class, 'assigned_by');
}
Then you can get the assignor using:
User::with('roles')->first() // a User instance with loaded `roles`
->pivot // the pivot model
->assignor; // the assigning User