Download the PHP package aaronbell1/laravel-csv-bulk-uploader without Composer
On this page you can find all versions of the php package aaronbell1/laravel-csv-bulk-uploader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-csv-bulk-uploader
Laravel CSV Bulk Uploader
The bulk uploader is intended to allow users to have a simple way to validate and upload records from a CSV directly to database.
The package makes use of Laravel validators to validate the data, and Laravel DB object to commit records to the Database.
Table of Contents
- Installation
- Creating bulk uploader
- Usage
Installation
You can install the package via composer:
If you are using Laravel < 5.5, you will need to add the following to your 'providers'
array in config/app.php
:
Creating bulk uploader
To create a new uploader, from the command line enter:
Where {name}
is the class name e.g. 'UserBulkUploader'
This will generate the class within app/Uploaders
folder.
There are 3 methods that are required to implement in your class:
rules
messages
saveRow
rules
The rules()
method represents validation rules as per a Laravel Form Request.
This must return an array with the rules required for a single row of your CSV data.
The field names are represented as a lowercase and underscored version of your column header value e.g. User first name
would be user_first_name
.
For example:
messages
The messages()
method represents validation rule messages as per Laravel Form Request Error Messages.
For example, you could overwrite one of the above messages using:
saveRow
The saveRow()
method is used to define how you process each row of data returned from the CSV.
The data is passed through as an array with the keys defined as the column headers converted to lowercase with underscores.
For example using the validation from above, you can access each row of data with:
Usage
In order to make use of the package you must ensure that you are using a .csv
file with a header row that includes a unique name for each column.
In the controller where you need to use the bulk uploader, you can either inject an instance of the class:
or create a new instance of it:
The uploader instance has the following methods:
load
isValid
save
redirectWithErrors
load
The load()
method accepts the path to the CSV file that you are working from.
isValid
The isValid()
method will use the rules as defined on the bulk uploader to check whether the CSV contains valid data.
This will return a boolean value.
If the data is not valid, you can easily redirect back with the errors to display them to the user as you require.
save
The save()
method will make use of the saveRow()
method defined on the bulk uploader to process your data.
This works as a database transaction so if it encounters any errors it will rollback the transaction and throw an appropriate exception, otherwise if successful it will be committed.
redirectWithErrors
The redirectWithErrors()
method will make use of Laravel Redirects to redirect the user back to the previous page with any errors stored in the session.
The method accepts the name of the data array which by default is data
.
You can make use of these errors in your blade file using the session helper: