In this post I diccuses how to export Million(s) records with Laravel using queue job Batching Before anything make sure to migrate batches table using: php artisan queue:batches-table php artisan migrate

In my controller I have this code: public function export() { $chunkSize = 10000; $usersCount = User::count(); $numberOfChunks = ceil($usersCount / $chunkSize); $folder = now()->toDateString().

In CreateUsersExportFile: class CreateUsersExportFile implements ShouldQueue { use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct( public $chunkSize, public $folder ) { } public function handle() { $users = User::query() ->take($this->chunkSize) ->get(); Storage::disk('local')->makeDirectory($this->folder); (new \Rap2hpoutre\FastExcel\FastExcel($this->usersGenerator($users))) ->export(storage_path("app/{$this->folder}/users.csv"), function ($user) { return [ 'id' => $user->id, 'name' => $user->id, 'email' => $user->id, //... ]; }); }} private function usersGenerator($users) { foreach ($users as $user) { yield $user; }}

In CreateUsersExportFile: class AppendMoreUsers implements ShouldQueue { use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct( public $chunkIndex, public $chunkSize, public $folder ) { } public function handle() { $users = User::query() ->skip($this->chunkIndex * $this->chunkSize) ->take($this->chunkSize) ->get() ->map(function ($user) { return [ $user->id, $user->name, $user->email, ]; }); $file = storage_path("app/{$this->folder}/users.csv"); $open = fopen($file, 'a+'); foreach ($users as $user) { fputcsv($open, $user); } fclose($open); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners