php-dominicana/laravel-model-export
| Install | |
|---|---|
composer require php-dominicana/laravel-model-export |
|
| Latest Version: | v0.0.3 |
| Laravel: | 12 |
| License: | MIT |
| Last Updated: | Jun 22, 2025 |
| Links: | GitHub · Packagist |
laravel-model-export
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().
Related Packages
Laravel package that checks and creates WordPress password hashes
Laravel countries, states, cities, currencies, languages and IP geolocation
Version History
| Version | Released | Laravel | License |
|---|---|---|---|
| v0.0.3 | ^12.18 | MIT | |
| v0.0.2 | ^12.18 | MIT | |
| v0.0.1 | ^12.18 | MIT |