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

zhxscript's avatar

Dispatching Nova Actions Manually

So I am looking to be able to dispatch Nova actions manually, either through a Modal observer or event listener..

This is my current code:

        try{
            $this->logEventData('Start Observer');//this does record

            $collection = Collection::wrap($website);

            $action = new APIResponse;

            $request = app(ActionRequest::class);
            $request->action = $request->query->set('action','observeraction');
            $fields = $request->resolveFields();

            $this->logEventData('Processing Observer');//this does record

            return  DispatchAction::forModels($request, $action, 'handle', $collection,$fields);//does appear to run

        }catch(Exception $e){

            Log::critical('Message Failed');//This never records

        }

Also as a test both the $request & collections have the data and worked. But there is NO ERROR Log anywhere...

My Action is not doing anything yet except outputting a message, Which works when access on the resource page from the dropdown.

This particular codes is from my observer, but I also tried in an Even Listener, and same result...

So has anyone been able to accomplish this or have any ideas?

0 likes
1 reply
stwinnie's avatar

I needed to use Nova Actions outside of Nova from my custom API, so here is the code I've came up with. Please note that $user has to be Laravel Nova User with sufficient privileges to run the action.

    private function postToActionsApi($actionClass, $resource, $model, $params, $user) {
        
        Nova::resourcesIn(app_path('Nova'));

        $collection = Collection::wrap($model);
        $action = new $actionClass;

        $actionRequest = app(ActionRequest::class);
        $actionRequest->user = $user;
        $actionRequest->setUserResolver(function () use ($user) {
            return $user;
        });
        $actionRequest = $actionRequest->replace($params);
        $actionRequest->route()->setParameter('resource',  $resource);
        $actionRequest->route()->pathInfo = '/nova-api/'.$resource.'/action';
        $actionRequest->action = $actionRequest->query->set('action', $action->uriKey());
        $actionRequest->action = $actionRequest->query->set('pivotAction', 'false');
    
        $fields = $actionRequest->resolveFields();

        return DispatchAction::forModels(
            $actionRequest, 
            $action, 
            'handle', 
            $collection, 
            $fields
        );
    }
2 likes

Please or to participate in this conversation.