cyber-duck/nova-import

此包已被弃用且不再维护。作者建议使用 cyber-duck/nova-import 包。

Laravel Nova Excel 导入按钮。

0.0.2 2019-05-29 09:34 UTC

This package is auto-updated.

Last update: 2021-10-26 09:17:42 UTC


README

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

Nova 导入

将数据导入资源。

screenshot

安装

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

composer require cyber-duck/nova-import

NovaServiceProvider 中注册工具

use Cyberduck\NovaImport\NovaImport;
...

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

使用方法

要使用此工具,您需要创建以下两项

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

  2. 创建一个自定义 Nova 操作文件

<?php

namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Cyberduck\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;

    /**
     * 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{ResoueceName} 格式,例如 ImportUsersImportCountries

请注意,它扩展了 Cyberduck\NovaImport\Actions\Action 而不是常规 Nova 操作,因为它不适用于模型!

许可证

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