Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

psylogic's avatar

Condional Nova Actions

Hello,

I have some actions on table row, but I need to display actions in condition.

I tried to use canSee()

(new SetThingInProgress())
                ->showOnTableRow()
                ->canSee(function ($req) {
                    return $this->resource->status == Thing::PENDING;
                }),

Well, $this->resource it returns only the Model like just an instance, so $this->resource->status it obviously returns NULL. so canSee() is not working for me.

If I want to access to the property resource I have to use canRun() which accepts two params (NovaRequest, Model)

(new SetThingInProgress())
                ->showOnTableRow()
                ->canRun(function ($req, Thing $thing) {
                    return $thing->status == Thing::PENDING;
                }),

But that will help just users to run this action in condition.

Any suggestion please, maybe I missed something in the doc.

0 likes
4 replies
bugsysha's avatar

I've always used approach where I decide within the action if that resource/model should be affected by the action.

psylogic's avatar

Well, in some cases it's okay to do that.. but for user experience, I don't want the user to click on something and respond back "Whoops action is not allowed".

I don't know why canSee() function doesn't have a resource as a second param, while canRun() does.

I don't know how to hide an action and display the other.

bugsysha's avatar

In my mind that does not make sense. But you know what you need.

cybersponge's avatar

I just had the same problem. I solved it by defining an additional function in my custom Action:

    public function enabled(bool $value)
    {
        $this->showOnIndex = false;
        $this->showOnDetail = $value;
        $this->showInline = $value;
        return $this;
    }

In my Resource i can then enable or disable the visibility of the action by passing ->enable([condition]):

    public function actions(NovaRequest $request)
    {
        return [
            (new Actions\RevokeDeviceAccess)->enabled($this->client_id_key !== null);
        ];
    }

When i use this function, i always put $showOnIndex to false to prevent confusion in the index overview, as otherwise several different resources with different activated actions can be selected.

2 likes

Please or to participate in this conversation.