You might already know this.
When we access the employees relationship like a property like this, you got a collection:
$record->employees;
If we want to work with a database query scoped by that $record, we do this:
$record->employees();
With that, I reckon the following might work:
return $record->employees()
->where(fn ($query) =>$query->wherePivotIn('role', ['pri_ass', 'sec_ass']))
->get()
->sum('pivot.spent_hours');
Or you can try this:
return $record->employees
->filter(fn ($employee) =>in_array($employee->pivot->role, ['pri_ass', 'sec_ass']))
->get()
->sum('pivot.spent_hours');