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

ale1981's avatar

Laravel Nova Actions

I am attempting to create an action that can both be run against a whole resource (standalone) and selected resources, is this possible or will I need two separate actions?

If I register the action as standalone it still appears on the detail view but does nothing when I run it. I have attempted the following in the handle function but it won't work as I am assuming if you set the action as standalone it doesn't get the models collection.

public function handle(ActionFields $fields, Collection $models)
{
    if ($models->isEmpty())
        $models = Customer::withOutstandingBalance()->get();

    foreach($models as $customer)
    {
        Mail::to('[email protected]')->send(new CustomerChaseOutstanding($customer));
    }
}
0 likes
5 replies
bobbybouwmann's avatar

From the docs:

If you have an action that does not require any resources / models to run, you may register it as a "standalone" action by chaining the standalone method when registering the action. This action always receives an empty collection of models in its handle method:

So the collection should be empty in this case. I would either expect that your query to retrieve the models is returning nothing or some error is thrown somewhere.

You can inspect the network requests in your browser and see if the ajax request for the action is working or not.

ale1981's avatar

I understand that the collection is empty if I set the action as standalone and it works for all resources.

The issue is I want it to run as standalone as well as being able to run it against a resource. Is that possible with one action?

ale1981's avatar
ale1981
OP
Best Answer
Level 3

If anybody else comes across this problem, the solution is to register the action twice although this caused an error that I reported here...

https://github.com/laravel/nova-issues/discussions/3369

public function actions(Request $request)
{
    return [
        Actions\SendPaymentReminder::make(),
        Actions\SendPaymentReminder::make()->standalone()
    ];
}



public function handle(ActionFields $fields, Collection $models)
{
    if ( $this->isStandalone() )
        $models = Customer::withOutstandingBalance()->get();

    foreach($models as $customer)
    {
        Mail::to('[email protected]')->send(new PaymentReminder($customer));
    }
}

Please or to participate in this conversation.