coderflex/laravel-csv

A Laravel livewire CSV Importer, to handle importing millions of rows without a hustle.


README

Laravisit Logo

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Introduction

Laravel CSV Package is a package created on top of Laravel livewire for easily handling imports with a simple API.

Installation

You can install the package via composer:

composer require coderflex/laravel-csv

Configuration

Publish and run the migrations with:

php artisan vendor:publish --tag="csv-migrations"
php artisan migrate

Add trait HasCsvImports to your User model.

Publish the config file with:

php artisan vendor:publish --tag="csv-config"

The following is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Default Layout
    |--------------------------------------------------------------------------
    |
    | This package plans on supporting multiple CSS frameworks. 
    | Currently, 'tailwindcss' is the default and only supported framework.
    |
    */
    'layout' => 'tailwindcss',

    /*
    |--------------------------------------------------------------------------
    | Max Upload File Size
    |--------------------------------------------------------------------------
    |
    | The default maximumum file size that can be imported by this
    | package is 20MB. If you wish to increase/decrease this value, 
    | change the value in KB below.
    |
    */
    'file_upload_size' => 20000,
];

The layout option is for choosing which CSS framework you are using and currently supports only tailwindcss. We are working on other CSS frameworks to implement in the future.

The file_upload_size is for validation rules, and it defines the maximum file size of uploaded files. You may also define this value from the livewire config file.

Optionally, you can publish the views using

php artisan vendor:publish --tag="laravel-csv-views"

Before Using this command, please take a look at this section below.

Usage

CSV Importer Component

Using this package is a breeze. To implement the importer in your project, simply include the following component inside a Blade view.

    <livewire:csv-importer :model="App\Models\YourModel::class"
                            :columns-to-map="['id', 'name', 'email', 'password']"
                            :required-columns="['id', 'name', 'email']"
                            :columns-label="[
                                'id' => 'ID',
                                'name' => 'Name',
                                'email' => 'Email Address',
                                'password' => 'Password',
                            ]"/>

Button Component

The Component uses alpinejs under the hood. To display an import button, include the x-csv-button component.

<x-csv-button>Import</x-csv-button>

To style the button, use the class attribute with Tailwind utility classes.

<x-csv-button 
        class="rounded py-2 px-3 bg-indigo-500 ..."
        type="button"
        ....>
    {{ __('Import') }}
</x-csv-button>

In TALL stack project

If you are using this package in a TALL Stack project, (Tailwindcss, Alpinejs, Laravel, Livewire) publish the vendor views to include Laravel-CSV in your project.

php artisan vendor:publish --tag="csv-views"

Then compile your assets.

npm run dev

In none TALL Stack project

If you are not using the TALL Stack, use the csv directives to add the necessary styles/scripts.

<html>
    ...
    <head>
        ...
        @csvStyles
    </head>
        ...
    <footer>
        ...
        @csvScripts
    </footer>
</html>

Using Queues

This package uses queues under the hood with PHP Generators to make it fast and efficient.

Create the batches table by running

php artisan queue:batches-table

Then, run the migration.

php artisan migrate

After that, set up the queues' configuration. Head to Laravel Queues Documentation to learn more.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Inspiration

This Package Was Inspired by codecourse video series. If you want to learn how this package was created, make sure to take a look at this video series

Credits

License

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