FilamentF
Filament10mo ago
morty

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
    {
        //
    }
}


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.

    protected function canCreate(): bool
    {
        return Gate::allows('create', [$this->getTable()->getModel(), $this->ownerRecord]);
    }
Was this page helpful?