anaseqal/nova-import

Laravel Nova 导入工具。

0.0.6 2020-08-26 16:13 UTC

This package is not auto-updated.

Last update: 2024-09-19 11:33:15 UTC


README

💡 建议: Nova v3.10.0+ 具有创建“独立”操作的能力,可以在不选择资源的情况下运行操作。您可以使用此包创建导入操作。有关教程,请参阅 这里

将数据导入资源。

Screenshot

安装

您可以通过 composer 将此包安装到使用 Nova 的 Laravel 应用中。

composer require anaseqal/nova-import

在您的 app/Providers/NovaServiceProvider.php 中注册该工具

use Anaseqal\NovaImport\NovaImport;

// ...

public function tools()
{
    return [
        new NovaImport,
        // ...
    ];
}

使用方法

要使用此工具,您需要创建以下两个东西

  1. 使用 Laravel Excel 为您的资源创建一个导入类。

  2. 创建一个自定义 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'),
        ];
    }
}
  1. 将操作注册到您的资源中
/**
 * 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} 的格式,例如 ImportUsersImportCountries

请注意,它扩展了 Anaseqal\NovaImport\Actions\Action 而不是普通的 Nova 操作,因为它不适用于模型!

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件