PHPackages                             akki/bulkexportcsv - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. akki/bulkexportcsv

ActiveLibrary[Queues &amp; Workers](/categories/queues)

akki/bulkexportcsv
==================

Export unlimited records into CSV

v3.0.0(2y ago)12200↓100%MITPHPPHP ^7.3|^8.0

Since Nov 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/AkshayGadekar/bulkExportCSV)[ Packagist](https://packagist.org/packages/akki/bulkexportcsv)[ Docs](https://github.com/AkshayGadekar/bulkExportCSV)[ RSS](/packages/akki-bulkexportcsv/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (0)

Export Unlimited Records into CSV
=================================

[](#export-unlimited-records-into-csv)

[![Latest Stable Version](https://camo.githubusercontent.com/4d6e3d80cec561e9288732a6b7abd7f2a2481201bc731bb040c7bb7490067f3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616b6b692f62756c6b6578706f7274637376)](https://packagist.org/packages/akki/bulkexportcsv)[![License](https://camo.githubusercontent.com/6e42b6832a1297b880290169a4ed858f597a0c080ac78bb0500b9db9b41b5bc0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616b6b692f62756c6b6578706f7274637376)](https://packagist.org/packages/akki/bulkexportcsv)

Introduction
------------

[](#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
------------

[](#installation)

Install **BulkExportCSV**:

```
composer require akki/bulkexportcsv
```

Publish the config file `config/bulkexportcsv.php`, model `App\Models\BulkExportCSV.php`, migration `bulk_export_csv` table, events and listeners:

```
php artisan vendor:publish --provider="Akki\BulkExportCSV\ServiceProvider"
```

Prepare Migration of queue tables:

```
php artisan queue:table
php artisan queue:batches-table
```

Miragte tables:

```
php artisan migrate
```

Usage
-----

[](#usage)

### Make a query

[](#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.

```
$query = \App\Models\User::query();
$query->with('serviceProvider')->where('country', 'IN');
```

### Make a JSON resource

[](#make-a-json-resource)

```
php artisan make:resource UserResource
```

UserResource.php:

```
public function toArray($request)
{
    return [
        'name' => $this->name,
        'email' => $this->email,
        'service_provider' => $this->serviceProvider->org_name,
        'contact_number' => $this->contact_number,
    ];
}
```

### Export into CSV

[](#export-into-csv)

`build` method will start the export CSV process.

```
$query = \App\Models\User::query();
$query->with('serviceProvider')->where('country', 'IN');

$resource_namespace = 'App\Http\Resources\UserResource';

$bulkExportCSV = \BulkExportCSV::build($query, $resource_namespace);
```

`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](https://github.com/AkshayGadekar/bulkExportCSV/tree/develop#download-csv) method.
But, Before exporting into CSV using `build` method, Make sure to fill up `config/bulkexportcsv.php` correctly which is shown below.

### Configuration

[](#configuration)

Edit `config/bulkexportcsv.php` to suit your needs.

```
/** @file config/bulkexportcsv.php */

return [
    /*
    * Number of Records to be fetched per job
    */
    'records_per_job' => 10000,

    /*
    * records will be fetched in chunks for better performance
    */
    'chunks_of_records_per_job' => 2,

    /*
    * Directory where CSV will be prepared inside storage folder
    */
    'dir' => 'app/public/exportCSV',

    /*
    * Database connection for bulk_export_csv table
    */
    'db_connection' => env('DB_CONNECTION', 'mysql'),

    /*
    * Queue connection for jobs
    */
    'queue_connection' => env('QUEUE_CONNECTION', 'database'),

    /*
    * Name of queue where job will be dispatched
    */
    'queue' => 'default',

    /*
    * Name of queue job batch
    */
    'batch_name' => 'Bulk Export CSV',

    /*
    * The number of seconds the job can run before timing out
    * null takes default value
    * The pcntl PHP extension must be installed in order to specify job timeouts
    */
    'job_timeout' => null,

    /*
    * if any job fails, it stops CSV preparation process
    * Decide whether partial CSV prepared should get deleted or not
    */
    'delete_csv_if_job_failed' => false
];
```

### Events

[](#events)

When CSV starts to get prepared throught queue jobs, these are typical events that happens: CSV starts to get prepared =&gt; Queue jobs gets completed =&gt; 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

[](#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:

```
[
    'jobs_id' => unique ID generated for an export CSV request
    'csv_name' => CSV file name
    'total_records' => total number of records exported
    'total_jobs' => total jobs required to export CSV
    'completed_jobs' => when export CSV starts, this column gets updated with number of completed jobs
    'progress' => the completion percentage of the CSV export
    'export_status' => export status of CSV as 'InProgress', 'Completed', 'Error' or 'Cancelled'
    'each_jobs_time' => time taken by each job processed
    'average_jobs_time' => average time all jobs taken
    'error' => Exception error if any job fails or 'BulkExportCSVSucceeded' or 'BulkExportCSVFailed' events threw exception
    'config' => bulk export configuration used for an export CSV request
    'user_id' => ID of auth user requesting export CSV
    'batch_id' => batch_id of job batching process
]
```

### Queue Configuration

[](#queue-configuration)

Make sure you have filled up `config/queue.php` correctly. Install [Supervisor](https://laravel.com/docs/8.x/queues#supervisor-configuration), 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:

```
php artisan queue:work --queue=bulkExportCSV,default
```

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'
-------------------------------------------------

[](#more-options-in-build-method-of-bulkexportcsv)

### Define Columns for Export CSV

[](#define-columns-for-export-csv)

By default, package takes columns names from json resource itself. But one can define custom columns as required:

```
$columns = ['Name', 'Email', 'Service Provider', 'Contact Number'];
$bulkExportCSV = \BulkExportCSV::build($query, $resource_namespace, $columns);
```

### Access Request Data in Resource

[](#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')`:

```
$user = auth()->user();
$data = ['user' => $user, 'request' => $request->all(), 'csv_info' => 'Toal Users on Platform'];
$columns = []; //if columns are defined as empty, then columns will be taken from json resource itself
$bulkExportCSV = \BulkExportCSV::build($query, $resource_namespace, $columns, $data);
```

JSON Resource:

```
public function toArray($request)
{
    $data = config('bulkexportcsv.data');
    $user = $data['user'];
    $request = $data['request'];

    return [
        'name' => $this->name,
        'email' => $this->email,
        'service_provider' => $this->when($user->role == 'admin', $this->serviceProvider->org_name??"-"),
        'contact_number' => $request['contact_number'],
    ];
}
```

`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
-------------

[](#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

[](#findbyjobsid)

To fetch record from "bulk\_export\_csv" table using jobs\_id, one can use `findByJobsId` method:

```
use Akki\BulkExportCSV\Models\BulkExportCSVModel;

$bulkExportCSVInfo = BulkExportCSVModel::findByJobsId($jobs_id);
```

### cancelExportCSVProcess

[](#cancelexportcsvprocess)

To cancel ongoing export csv process, one can use `cancelExportCSVProcess` method:

```
use Akki\BulkExportCSV\Models\BulkExportCSVModel;

BulkExportCSVModel::cancelExportCSVProcess($jobs_id);
```

Download CSV
------------

[](#download-csv)

If one wants to download CSV directly instead of going though queue job process, then use `download` method:

```
return \BulkExportCSV::download($query, $resource_namespace);
```

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
---------------------

[](#installation-in-lumen)

Install **BulkExportCSV**:

```
composer require akki/bulkexportcsv
```

Service provider should be registered manually as follow in `bootstrap/app.php` with enabling some additional required options:

```
// regiser service provider
$app->register(Akki\BulkExportCSV\ServiceProvider::class);
// Enable Facades
$app->withFacades();
// Enable Eloquent
$app->withEloquent();
// Enable bulk export configuration
$app->configure('bulkexportcsv');
// BulkExportCSV class alias
if (!class_exists('BulkExportCSV')) {
    class_alias('Akki\\BulkExportCSV\\Facades\\BulkExport', 'BulkExportCSV');
}
// EloquentSerialize class alias
if (!class_exists('EloquentSerialize')) {
    class_alias('AnourValar\EloquentSerialize\Facades\EloquentSerializeFacade', 'EloquentSerialize');
}
```

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:

```
$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);
// declare path to storage folder
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');
```

If one gets error `Target [Illuminate\Contracts\Routing\ResponseFactory] is not instantiable`, add this in `AppServiceProvider.php`:

```
public function register()
{
    $this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() {
        return new \Laravel\Lumen\Http\ResponseFactory();
    });
}
```

Copy the required files:

```
mkdir -p config
cp vendor/akki/bulkexportcsv/src/config/bulkexportcsv.php config/bulkexportcsv.php
cp vendor/akki/bulkexportcsv/src/Models/BulkExportCSV.txt app/Models/BulkExportCSV.php
cp vendor/akki/bulkexportcsv/src/database/migrations/create_bulk_export_csv_table.txt database/migrations/2023_01_01_000000_create_bulk_export_csv_table.php
cp vendor/akki/bulkexportcsv/src/Events/BulkExportCSVStarted.txt app/Events/BulkExportCSVStarted.php
cp vendor/akki/bulkexportcsv/src/Events/BulkExportCSVJobCompleted.txt app/Events/BulkExportCSVJobCompleted.php
cp vendor/akki/bulkexportcsv/src/Events/BulkExportCSVSucceeded.txt app/Events/BulkExportCSVSucceeded.php
cp vendor/akki/bulkexportcsv/src/Events/BulkExportCSVFailed.txt app/Events/BulkExportCSVFailed.php
cp vendor/akki/bulkexportcsv/src/Listeners/ListenBulkExportCSVStarted.txt app/Listeners/ListenBulkExportCSVStarted.php
cp vendor/akki/bulkexportcsv/src/Listeners/ListenBulkExportCSVJobCompleted.txt app/Listeners/ListenBulkExportCSVJobCompleted.php
cp vendor/akki/bulkexportcsv/src/Listeners/ListenBulkExportCSVSucceeded.txt app/Listeners/ListenBulkExportCSVSucceeded.php
cp vendor/akki/bulkexportcsv/src/Listeners/ListenBulkExportCSVFailed.txt app/Listeners/ListenBulkExportCSVFailed.php
```

copy `queue:table`, `queue:batches-table` from laravel itself, migrate the tables:

```
php artisan migrate
```

Now you can follow the same [Usage](https://github.com/AkshayGadekar/bulkExportCSV#usage) mentioned for Laravel above.

Learning Resource
-----------------

[](#learning-resource)

There is a video series available on youtube to know how to use this package, [Click Here](https://www.youtube.com/watch?v=NacyPrRzxeQ&list=PLlE-aom9wrdk7096yCUYh9t-ob1pY_QYp).

Contribution
------------

[](#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)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Total

11

Last Release

1085d ago

Major Versions

v0.0.3 → v1.0.02022-12-04

v1.3.0 → v2.0.02023-03-11

v2.1.1 → v3.0.02023-05-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/a7ba9ff9d17b04d536ef7baf67b01fd3d61650759ab062339cb5c9f795fa74c9?d=identicon)[AkshayGadekar](/maintainers/AkshayGadekar)

---

Top Contributors

[![AkshayGadekar](https://avatars.githubusercontent.com/u/31556812?v=4)](https://github.com/AkshayGadekar "AkshayGadekar (21 commits)")

---

Tags

laravelexportcsvqueuejobbulkakshayExportCsvakki

### Embed Badge

![Health badge](/badges/akki-bulkexportcsv/health.svg)

```
[![Health](https://phpackages.com/badges/akki-bulkexportcsv/health.svg)](https://phpackages.com/packages/akki-bulkexportcsv)
```

###  Alternatives

[imtigger/laravel-job-status

Laravel Job Status

5272.1M2](/packages/imtigger-laravel-job-status)[mpbarlow/laravel-queue-debouncer

A wrapper job for debouncing other queue jobs.

63714.4k1](/packages/mpbarlow-laravel-queue-debouncer)[yajra/laravel-datatables-export

Laravel DataTables Queued Export Plugin.

341.9M3](/packages/yajra-laravel-datatables-export)[pmatseykanets/artisan-beans

Easily manage your Beanstalkd job queues right from the Laravel artisan command

4482.1k](/packages/pmatseykanets-artisan-beans)[renoki-co/horizon-exporter

Export Laravel Horizon metrics using this Prometheus exporter.

24152.7k](/packages/renoki-co-horizon-exporter)[maqe/laravel-sqs-fifo

Laravel package that enables support for SQS FIFO Queue

15137.2k](/packages/maqe-laravel-sqs-fifo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
