hillbilisim / nova-import
Laravel Nova 4 导入工具。
v1.0.0
2024-06-06 21:03 UTC
Requires
- php: >=7.1.0
- maatwebsite/excel: *
README
💡 建议: Nova v3.10.0+ 具有创建“独立”动作的能力,可以在不选择资源的情况下运行动作。你可以不使用此包创建导入动作。 教程
将数据导入资源。
安装
您可以通过 composer 将此包安装到使用 Nova 的 Laravel 应用程序中。
composer require anaseqal/nova-import
在您的 app/Providers/NovaServiceProvider.php 中注册此工具
use Anaseqal\NovaImport\NovaImport; // ... public function tools() { return [ new NovaImport, // ... ]; }
用法
要使用此工具,您需要创建两件事情
-
使用 Laravel Excel 为您的资源创建一个导入类。
-
创建一个自定义 Nova 动作文件
<?php namespace App\Nova\Actions; use Illuminate\Bus\Queueable; use Anaseqal\NovaImport\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\UsersImport; use Maatwebsite\Excel\Facades\Excel; class ImportUsers extends Action { use InteractsWithQueue, Queueable, SerializesModels; /** * Indicates if this action is only available on the resource detail view. * * @var bool */ public $onlyOnIndex = true; /** * Get the displayable name of the action. * * @return string */ public function name() { return __('Import Users'); } /** * @return string */ public function uriKey() :string { return 'import-users'; } /** * Perform the action. * * @param \Laravel\Nova\Fields\ActionFields $fields * @return mixed */ public function handle(ActionFields $fields) { Excel::import(new UsersImport, $fields->file); return Action::message('It worked!'); } /** * Get the fields available on the action. * * @return array */ public function fields() { return [ File::make('File') ->rules('required'), ]; } }
- 将动作注册到您的资源中
/** * Get the actions available for the resource. * * @param \Illuminate\Http\Request $request * @return array */ public function actions(Request $request) { return [new Actions\ImportUsers]; }
动作名称必须使用 Import{ResourceName} 的格式,例如 ImportUsers 或 ImportCountries。
请注意,它继承自 Anaseqal\NovaImport\Actions\Action 而不是普通的 Nova Actions,因为它不应用于模型!
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件。