s-patompong/nova-import
一个Laravel Nova导入工具。
1.0.0
2024-02-21 06:30 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操作,因为它不应用于模型!
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。