php-dominicana / laravel-model-export
This is a simple package to export model to csv, json
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 1
Open Issues: 0
Type:laravel-package
pkg:composer/php-dominicana/laravel-model-export
Requires
- ext-http: *
- barryvdh/laravel-dompdf: ^3.1
- illuminate/support: ^12.18
- spatie/simple-excel: ^3.7
- spatie/temporary-directory: ^2.3
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.22
- orchestra/testbench: *
- pestphp/pest: ^3.8
- rector/rector: ^2.0
- spatie/phpunit-snapshot-assertions: ^5.2
README
A lightweight Laravel package to export Eloquent model data to CSV, excel, with support for low-memory, lazy exports and a clean API via an Exportable
trait and query macro.
📦 Features
- Export model data to CSV using a simple method call
- Export via query chaining (
User::where(...)->exportToExcel()
) - Low memory usage via
lazyById()
- Customizable export paths
🚀 Installation
composer require php-dominicana/laravel-model-export
🛠 Usage
Export all records (static call)
User::exportToExcel(); // uses default filename and $exportable columns
Export filtered data via query
User::where('active', true)
->orderBy('name')
->exportToExcel(); // exports only active users
Export to custom path
User::exportToExcel(storage_path('exports/users.csv'));
📁 File Output
-
Files are exported to /storage/app by default unless a custom path is provided.
-
Filenames follow this format: export_ModelName_TIMESTAMP.csv
Export to browser
User::streamDownload();
Export to JSON
User::exportToJson();
Export to PDF
User::exportToPDF();
Customize the PDF View (Optional)
To let users override your default PDF view, instruct them to publish it:
php artisan vendor:publish --tag=model-export-views
🧠 How It Works
-
The Exportable trait adds a static exportToExcel() method for convenience.
-
A macro is registered on Eloquent\Builder so that you can chain ->exportToExcel() on queries.
-
Under the hood, Spatie’s SimpleExcelWriter handles the CSV generation.
-
Memory-efficient thanks to Laravel’s lazyById().