Search code examples
laravellaravel-nova

Nova action with custom message for confirmation - tool required?


In the Nova docs, it is indicated that fields can be used to require input from a user before dispatching an action.

https://nova.laravel.com/docs/3.0/actions/defining-actions.html#action-fields

public function fields()
{
    return [
        Text::make('Subject'),
    ];
}

These fields are referenced in handle:

public function handle(ActionFields $fields, Collection $models)
{
    foreach ($models as $model) {
        (new AccountData($model))->send($fields->subject);
    }
}

It is unclear from the docs what AccountData should be. When I instantiate a model, it tells me send is undefined. What is the simplest way to get a modal to popup that includes the defined fields?


Solution

  • What you should do is to create a new action with artisan nova:action and in that actions fields method define the fields user should fill out. They work in the same way as normal fields in a resource. Then inside the actions method inside the resource where you want the action, you add it to the return array in actions method.

    eg: inside resource:

       public    function actions(Request $request)
       {
            return [
                QuickImportAction::make($this->resource, $request)->standalone()
        ];
        )
    

    And then inside the Nova/Actions/ after action is generated:

     public function handle(ActionFields $fields, Collection $models)
        {
            if($models->count()>0) return Action::danger("Replace in filters is not ready, unselect and replace in all");
        }
    
        /**
         * Get the fields available on the action.
         *
         * @return array
         */
        public function fields()
        {
            $replace = "";
    
            return [
                Heading::make("<div class=' text-secondary'> NOTE! only selected filters rows will be replaced</div>")
                       ->textAlign('center')->asHtml(),
    ];
    )
    

    EDIT: Sorry, let me reply to your actual question. The

    (new <class>)->method
    

    Is the same as:

    $model = new MyModel();
    $model->method()
    

    So its different from application to application.