Laravel 12
No release in over a year
This is a simple package to export model to csv, json
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

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