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

v0.0.3 2025-06-22 19:18 UTC

This package is auto-updated.

Last update: 2025-10-22 20:35:54 UTC


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().