Skip to content

AnalogRepublic/laravel-nova-csv-import

 
 

Repository files navigation

Laravel Nova CSV Import

Latest Stable Version Total Downloads License

A simple CSV import tool for Laravel Nova that allows you to upload a CSV file via Nova and choose which Nova resource to import it to.

The import process lets you choose how to map the relevant columns from your uploaded file to the attributes on your models, with a nice summary at the end of what worked and what didn't

This package builds on top of the great work done by Sparclex with the nova-import-card package.

Laravel Nova CSV Import Screenshot

Sponsorship

CSV Import is completely free to use. I've built it in my own time to fill my own needs and I also support it in my own time. If you'd like to show your appreciation for that, I do accept donations via GitHub.

Thank you 🙏

Installation

Install via Composer:

composer require simonhamp/laravel-nova-csv-import --with-all-dependencies

Once installed, you must register the component in your app's NovaServiceProvider (usually in app/Providers/NovaServiceProvider.php):

namespace App\Providers;

use SimonHamp\LaravelNovaCsvImport\LaravelNovaCsvImport;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public function tools()
    {
        return [
            new LaravelNovaCsvImport,
        ];
    }
}

Options

By default, all of your Nova Resources will be available for import. However, there are a number of ways that you can explicitly limit what's available for importing.

public static $canImportResource = false;
Default: true
Add this static property to your Resource to prevent it from showing up in the Nova CSV Import tool interface.

public static function canImportResource($request): bool
Define a canImportResource method to use more complex logic to decide if this Resource can be shown during import. If defined, this takes precedence over the $canImportResource property.

public static function excludeAttributesFromImport(): array
Default: []
Define a excludeAttributesFromImport method that returns an array of attribute names that you want to exclude from being visible in the import tool for this Resource.

Example

// App\Nova\User
public static function canImportResource(Request $request)
{
    return $request->user()->can("create", self::$model);
}

public static function excludeAttributesFromImport()
{
    return ['password'];
}

Importer Class

This package uses maatwebsite/excel behind the scenes to handle the actual import. You can find more information about how importing works here.

You can define your own importer class by providing the relevant class name in your published copy of this package's config file.

First, publish the config file:

php artisan vendor:publish --tag=nova-csv-import

Then, define and register your own importer class:

<?php

return [
    'importer' =>  App\Utilities\Importer::class,
];

Testing

We need tests! Can you help? Please consider contributing.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see LICENSE for more details.

Packages

No packages published

Languages

  • Vue 51.2%
  • PHP 45.1%
  • JavaScript 3.6%
  • CSS 0.1%