Download the PHP package akki/bulkexportcsv without Composer
On this page you can find all versions of the php package akki/bulkexportcsv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bulkexportcsv
Export Unlimited Records into CSV
Introduction
This package will help you in exporting unlimited data using laravel's eloquent query and json resource. It is based on queue batching of laravel. It supports all databases eloquent supports.
Installation
Install BulkExportCSV:
Publish the config file config/bulkexportcsv.php
, model App\Models\BulkExportCSV.php
, migration bulk_export_csv
table, events and listeners:
Prepare Migration of queue tables:
Miragte tables:
Usage
Make a query
Prepare an eloquent query, make sure query does not have get(), first(), skip(), limit() methods. By Default package will export all records query gives.
Make a JSON resource
UserResource.php:
Export into CSV
build
method will start the export CSV process.
build
method returns BulkExportCSV
modal which is pointed to published bulk_export_csv
table, it gives all information regarding CSV request. If data to export is less, one can use download method.
But, Before exporting into CSV using build
method, Make sure to fill up config/bulkexportcsv.php
correctly which is shown below.
Configuration
Edit config/bulkexportcsv.php
to suit your needs.
Events
When CSV starts to get prepared throught queue jobs, these are typical events that happens: CSV starts to get prepared => Queue jobs gets completed => Queue batching has completed and CSV is prepared successfully or Queue batching has stopped because of exception.
To handle this each event, package publishes events and their respective listeners:
1) BulkExportCSVStarted: This event gets triggered when CSV starts to get prepared
2) BulkExportCSVJobCompleted: This event gets triggered for each queue job when that particular job gets completed.
3) BulkExportCSVSucceeded: This event gets triggered when CSV gets perpared successfully, i.e. queue batching has successfully completed.
4) BulkExportCSVFailed: This event gets triggered when any particular queue job throws an exception and so stops queue batching process, so CSV does not get prepared successfully.
Each of these events gets BulkExportCSV
modal as a parameter. You can broadcast this events, we recommend using ShouldBroadcastNow
interface so event gets broadcast in sync with queue jobs.
bulk_export_csv table
When CSV starts to get prepared, you can access its current status using published "bulk_export_csv" table which has following columns. BulkExportCSV
modal points to this table:
Queue Configuration
Make sure you have filled up config/queue.php
correctly. Install Supervisor, in its configuration file, command must mention queue name used for bulkExportCSV. For example, in config/bulkexportcsv.php
if queue
name is bulkExportCSV
then command must be:
Of course, You can specify which queues queue worker should process by priority depending on your needs. If you are broadcasting events using ShouldBroadcast
interface make sure to add its queue name here too on which it is broadcasting.
More Options in 'build' method of 'BulkExportCSV'
Define Columns for Export CSV
By default, package takes columns names from json resource itself. But one can define custom columns as required:
Access Request Data in Resource
Often times, we need authenticated user data or request data in json resource. As export CSV happens in background, there is no access to request, but one can send data to json resource or even eloquent model accessors or event listeners by using config('bulkexportcsv.data')
:
JSON Resource:
csv_info
key in data array is specifically accessible on BulkExportCSV
model as $bulkExportModal->config->csv_info
.
Make sure to restart queue workers, if one does changes in json resource.
Extra Methods
Package uses Akki\BulkExportCSV\Models\BulkExportCSVModel
model to access "bulk_export_csv" table. This model extends from published App\Models\BulkExportCSV
model.
findByJobsId
To fetch record from "bulk_export_csv" table using jobs_id, one can use findByJobsId
method:
cancelExportCSVProcess
To cancel ongoing export csv process, one can use cancelExportCSVProcess
method:
Download CSV
If one wants to download CSV directly instead of going though queue job process, then use download
method:
Here, one can also pass Columns
and Data
parameters similar to build
method. download
method creates CSV on the fly i.e. without writing CSV on the server disk and returns downloadable CSV file to the browser. In frontend side, to force browser to download CSV directly, you need to let browser call the API, you can use location.href
for it. If one prefers to call API from AJAX then in response download
method gives content of CSV, so in frontend one can make CSV using blob.
If one is to use download
method only, then there is no need of any configuration. One can use build
and download
method based on their prefer choice, if data to export is huge which one can know using count()
method on eloquent, then better to go with build
method otherwise download
method can also be right choice.
Installation in LUMEN
Install BulkExportCSV:
Service provider should be registered manually as follow in bootstrap/app.php
with enabling some additional required options:
If one gets error 'ReflectionException' with message 'Class path.storage does not exist'
,
declare storage folder path in bootstrap/app.php
right after $app
definition:
If one gets error Target [Illuminate\Contracts\Routing\ResponseFactory] is not instantiable
,
add this in AppServiceProvider.php
:
Copy the required files:
copy queue:table
, queue:batches-table
from laravel itself, migrate the tables:
Now you can follow the same Usage mentioned for Laravel above.
Learning Resource
There is a video series available on youtube to know how to use this package, Click Here.
Contribution
You can contribute to this package by discovering bugs and opening issues. Please, add to which version of package you create pull request or issue. (e.g. [1.0.0] Fatal error on build
method)