A Laravel Nova tool to allow for placing actions in the Nova toolbar, detached from the checkbox selection mechanism.
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
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
];
}
...
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'),
];
}
...
The MIT License (MIT). Please see License File for more information.