cyber-duck/nova-import

This package is abandoned and no longer maintained. The author suggests using the cyber-duck/nova-import package instead.

Laravel Nova Excel Import button.

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+ has ability to create "standalone" actions for running actions without selecting resources. you can create import action without using this package. Tutorial

Nova Import

Import data to resources.

screenshot

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require cyber-duck/nova-import

Register the tool in NovaServiceProvider:

use Cyberduck\NovaImport\NovaImport;
...

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

Usage

To use this tool, you need to create two things:

  1. Create an import class for your resource using Laravel Excel.

  2. Create a custom Nova Action file:

<?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. Register the action into your resource:
/**
 * Get the actions available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function actions(Request $request)
{
    return [new Actions\ImportUsers];
}

Action Name must use the Format of Import{ResoueceName}, for example ImportUsers or ImportCountries.

Please note that it extends Cyberduck\NovaImport\Actions\Action not normal Nova Actions because it doesn't applies on models!

License

The MIT License (MIT). Please see License File for more information.