Php Laravel Nova v2.8.0:定制动作标签
有没有一种简单的方法可以根据我的资源概述自定义操作标签? 产品资源Php Laravel Nova v2.8.0:定制动作标签,php,laravel,laravel-nova,Php,Laravel,Laravel Nova,有没有一种简单的方法可以根据我的资源概述自定义操作标签? 产品资源 //App\Nova\Product.php namespace App\Nova; use Illuminate\Http\Request; use App\Nova\Actions\UploadProdcuts as UploadProdcuts; class Product extends Resource { //... /** * Get the actions available fo
//App\Nova\Product.php
namespace App\Nova;
use Illuminate\Http\Request;
use App\Nova\Actions\UploadProdcuts as UploadProdcuts;
class Product extends Resource
{
//...
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
new UploadProdcuts
];
}
}
上传产品行动
//App\Nova\Actions\UploadProdcuts.php
namespace App\Nova\Actions;
use Illuminate\Bus\Queueable;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\File;
use App\Imports\ProductsImport;
use Maatwebsite\Excel\Facades\Excel;
class UploadProdcuts extends Action
{
use InteractsWithQueue, Queueable, SerializesModels;
//public $onlyOnDetail = true;
//public $onlyOnIndex = true;
/**
* Perform the action on the given models.
*
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models)
{
Excel::import(new ProductsImport, request()->file('file'));
return Action::message('Products Uploaded Successfully!');
}
/**
* Get the fields available on the action.
*
* @return array
*/
public function fields()
{
return [
File::make('File')->rules('required', 'max:50000', 'mimetypes:application/csv,application/excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
];
}
}
您可以在类中设置
$name
属性,也可以添加函数。如果您查看Nova的Action类(vendor/laravel/Nova/src/Actions/Action.php)中的name
函数:
/**
*获取操作的可显示名称。
*
*@返回字符串
*/
公共函数名()
{
返回$this->name?:Nova::人性化($this);
}
因此,您可以在类中设置如下属性:
类UploadProdcuts扩展操作
{
public$name='My Action';
}
或者只需添加名称
函数:
/**
*获取操作的可显示名称。
*
*@返回字符串
*/
公共函数名():字符串
{
返回u uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
}
请注意,您的类名中有一个输入错误。您将其命名为UploadProdcuts
,而不是UploadProducts