Blog Detail

27

Sep
Easily Import Thousands of Rows with Laravel CSV Importer cover image

arrow_back Easily Import Thousands of Rows with Laravel CSV Importer

Laravel CSV Package is a package created on top of the 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

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"

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 the livewire config file.

Optionally, you can publish the views using

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

Usage

CSV Importer Component
Using this package is really simple, all you need to do is implement 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',
                            ]"/>

Button Component

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

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

If you want to style it, you may use the class attribute or any attribute you want.

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

In the TALL stack project

If you are using this package in a TALL Stack project, (Tailwindcss, Alpinejs, Laravel, Livewire) All 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, that 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>

For more details, please visit Github.

Closing Notes

If you are a business and want to leverage Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your bespoke requirements, you must discover an impeccable team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.

Published at : 27-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project