springboardVR / nova-detached-actions

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Nova Detached Actions Tool

A Laravel Nova tool to allow for placing actions in the Nova toolbar, detached from the checkbox selection mechanism.

⚠️ Keep in mind, since the action is detached from the row selection checkboxes in the resource table, you will not have a collection of models to iterate over. Detached actions are intended to be independent of the selction in the table. ⚠️ Also, keep in mind, pivot actions are not supported and have not been tested.

screenshot

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require gobrightspot/nova-detached-actions

The tool will be automatically registered via the ToolServiceProvider - Thanks @milewski

Usage

Create a custom Nova Action file:

php artisan nova:action ExportUsers

Instead of extending the ExportUsers class with the Laravel\Nova\Actions\Action class, swap it with the Brightspot\Nova\Tools\DetachedActions\DetachedAction class.

Since we won't receive a collection of $models, you can remove the variable from the handle method, so that the signature is public function handle(ActionFields $fields).

You can also customize the button label, by overriding the label() method. If you do not override the label, it will 'humanize' the class name, in the example ExportUsers would become Export Users.

By default the detached action will only appear on the Index Toolbar.

If you want to also show the action on the resource index view (when users select a row with a checkbox), set the $public $showOnIndex = true; If you want to also show the action on the resource detail view (when user selects the action from the dropdown), set the $public $showOnDetail = true;

Here's a full example:

<?php

namespace App\Nova\Actions;

use Brightspot\Nova\Tools\DetachedActions\DetachedAction;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\ActionFields;

class ExportUsers extends DetachedAction
{
    use InteractsWithQueue, Queueable, SerializesModels;
    
    /**
     * Get the displayable label of the button.
     *
     * @return string
     */
    public function label()
    {
        return __('Export Users');
    }

    /**
     * Perform the action.
     *
     * @param  ActionFields  $fields
     *
     * @return mixed
     */
    public function handle(ActionFields $fields)
    {
        // Do work to export records

        return DetachedAction::message('It worked!');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [];
    }
}

Register the action on your resource:

<?php
...
    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [
            new App\Nova\Actions\ExportUsers
        ];
    }
...

Usage with the Laravel Nova Excel DownloadExcel action

You can easily integrate the DetachedAction tool with the Laravel Nova Excel DownloadExcel action by simply passing some additional data along using withMeta().

<?php
...
    /**
      * Get the actions available for the resource.
      *
      * @param  \Illuminate\Http\Request  $request
      * @return array
      */
     public function actions(Request $request)
     {
         return [
             (new DownloadExcel)->withHeadings()->askForWriterType()->withMeta([
                 'detachedAction' => true,
                 'label' => 'Export',
                 'showOnIndexToolbar' => true
             ])->confirmButtonText('Export'),
         ];
     }
 ...

License

The MIT License (MIT). Please see License File for more information.

About

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

License:MIT License


Languages

Language:PHP 60.7%Language:Vue 20.6%Language:JavaScript 16.6%Language:CSS 2.2%