digital-creative / nova-detached-actions
一个Laravel Nova工具,允许在Nova工具栏中放置与复选框选择机制分离的操作。
v1.1.1
2020-05-09 16:50 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2024-09-10 02:04:36 UTC
README
一个Laravel Nova工具,允许在Nova工具栏中放置与复选框选择机制分离的操作。
⚠️ 注意,由于操作与资源表中的行选择复选框分离,您将不会有一个模型集合来迭代。分离的操作旨在独立于表中的选择。
安装
您可以通过Composer将包安装到使用Nova的Laravel应用中
composer require digital-creative/nova-detached-actions
使用方法
创建自定义Nova操作文件
php artisan nova:action ExportUsers
不是通过扩展Laravel\Nova\Actions\Action
类来扩展ExportUsers
类,而是将其替换为DigitalCreative\DetachedActions\DetachedAction
类。
由于我们不会接收到$models
集合,您可以从handle
方法中删除变量,以便签名是public function handle(ActionFields $fields)
。
您还可以通过覆盖label()
方法来自定义按钮标签。如果您不覆盖标签,它将“人性化”类名,例如,ExportUsers
将变为Export Users
。
下面是一个完整的示例
<?php namespace App\Nova\Actions; use DigitalCreative\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 []; } }
在您的资源上注册操作
... /** * 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 ]; } ...
与Laravel Nova Excel DownloadExcel
操作一起使用
您可以通过使用withMeta()
传递一些额外数据来轻松地将DetachedAction
工具与Laravel Nova Excel的DownloadExcel
操作集成。
... /** * 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' ])->confirmButtonText('Export'), ]; } ...
许可协议
MIT许可协议(MIT)。请参阅许可文件以获取更多信息。