Skip to content

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

License

Notifications You must be signed in to change notification settings

coderflexx/laravel-csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 package, and it handles importing thousands of records without any issues.

Installation

You can install the package via composer:

composer require coderflex/laravel-csv

Configuration

You can publish and run the migrations with:

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

You can publish the config file with:

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

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Default Layout
    |--------------------------------------------------------------------------
    |
    | This package came with multiple layouts to serve your need, and
    | currently it supports "tailwindcss" and "bootstrap", by default
    | the layout is tailwind.
    | currently support: "tailwindcss", "bootstrap"
    |
    */
    'layout' => 'tailwindcss',

    /*
    |--------------------------------------------------------------------------
    | Max Upload File Size
    |--------------------------------------------------------------------------
    |
    | This package came with file validation for uploaded files,
    | and by default the file should not be greater than 20MB. If
    | you wish to increase/decrease this value, you may change the
    | value below.
    | Note that the value is defined by "KB".
    |
    */
    'file_upload_size' => 20000,
];

The layout option is for choosing which CSS Framework you are using, currently supports only tailwindcss, and we're working on other CSS frameworks to implement in the future.

The file_upload_size is for validation rules, and it helps define the file size of the uploaded files, or. You can define this one from livewire config file.

Optionally, you can publish the views using

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

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

Usage

CSV Importer Component

Using this package, is really simple, all what you need to do is implementing the component inside your desired file.

    <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',
                            ]"/>
Props Type Description
model string Fully qualified name of the model wants to import to
columns-to-map array Accept Columns need to be imported in the db
required-columns array Accept Columns need to be required while importing
columns-label array Accept Column Label of the required columns for the message

Button Component

The Component using alpinejs under the hood, If you want to use add the import button, you may use x-csv-button component.

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

If you want to style it, you can use the class attribute, or any attribute you want really

<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) All what you need to do is publish the vendor views

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

Then compile your assets, to add the additional classes, came with the component.

npm run dev

In none TALL Stack project

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

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

Using Queues

This package is using queues, under the hood with PHP Generators, to make it works fast and efficient.

You need first to create the batches table

php artisan queue:batches-table

Then run the migration

php artisan migrate

After that, you need to set up the queues' configuration. You may head into 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, and 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.