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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners