Is it possible to pass the parent record to the gate of a relation manager?

I have a Work Orders resource and on the edit page of each is a relation manager for Work Order Notes. I have policies for both models. I only want to allow a work order note to be created on work orders that the logged in user owns. The problem is that Filament checks a create method on the WorkOrderNotePolicy that doesn't pass in anything other than the user. Eg.
class WorkOrderNotePolicy
{
public function create(User $user): bool
{
//
}
}
class WorkOrderNotePolicy
{
public function create(User $user): bool
{
//
}
}
Is there a way that I can pass in the parent/owner record to this method on the policy?
Solution:
This is what I ended up doing on the relation manager. There are several canAction methods on the relation manager and I override the one's I need. Not sure if this is the best way to do this though but it works. ```php protected function canCreate(): bool {...
Jump to solution
3 Replies
morty
mortyOP3w ago
Another use case would be making sure noone can create a note on locked work orders. Interesting, filament actually isn't checking a create method on the WorkOrderNotePolicy but rather on the WorkOrderPolicy. This is weird.
Solution
morty
morty3w ago
This is what I ended up doing on the relation manager. There are several canAction methods on the relation manager and I override the one's I need. Not sure if this is the best way to do this though but it works.
protected function canCreate(): bool
{
return Gate::allows('create', [$this->getTable()->getModel(), $this->ownerRecord]);
}
protected function canCreate(): bool
{
return Gate::allows('create', [$this->getTable()->getModel(), $this->ownerRecord]);
}
Dennis Koch
Dennis Koch3w ago
Yes, the can methods are probably the only way.

Did you find this page helpful?