You're correct: when creating a related record via a Filament RelationManager, the mutateFormDataBeforeCreate method on your main resource's page (like CreateContract) is not called. Instead, RelationManagers have their own lifecycle hooks.
To mutate the form data before creating a record from a RelationManager, you should override the mutateFormDataBeforeCreate method on the RelationManager itself.
Here’s how you can do it:
use Filament\Resources\RelationManagers\RelationManager;
class ContractsRelationManager extends RelationManager
{
// ... your existing code ...
protected function mutateFormDataBeforeCreate(array $data): array
{
// Add the creator's user ID
$data['added_by'] = auth()->id();
return $data;
}
}
Where to put this:
- If your RelationManager is called
ContractsRelationManager, add the method there. - This method will be called whenever a new Contract is created via the RelationManager (e.g., from the Account view).
Summary:
- Use
mutateFormDataBeforeCreatein yourRelationManagerclass to mutate data before creation in that context. - Use it in your
CreateContractpage for standalone contract creation.
Reference:
Filament Docs: Relation Managers - Mutating Form Data