petersowah / laravel-factory-dumps
Export Laravel factory data to CSV or Excel files
Fund package maintenance!
petersowah
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- league/csv: ^9.0
- maatwebsite/excel: ^3.1
- nesbot/carbon: ^2.63||^3.8.4
- spatie/laravel-package-tools: ^1.19
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.13
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^2.34||^3.7.4
- pestphp/pest-plugin-arch: ^2.7||^3.0
- pestphp/pest-plugin-laravel: ^2.4||^3.1
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan: ^1.3||^2.1.8
- phpstan/phpstan-deprecation-rules: ^1.2||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
Easily export your Eloquent models to Excel and CSV formats.
This package helps with exporting factory generated data and Eloquent collections to CSV or Excel formats. It provides a simple and intuitive way to export your data with support for custom column selection and renaming.
Installation
You can install the package via composer:
composer require petersowah/laravel-factory-dumps
You can publish the config file with:
php artisan vendor:publish --tag="laravel-factory-dumps-config"
This is the contents of the published config file:
return [ 'path' => env('FACTORY_DUMPS_PATH', __DIR__.'/../workbench/database/dumps'), ];
Usage
Basic Usage
- Import the
ExportableFactory
trait in your model:
use PeterSowah\LaravelFactoryDumps\Traits\ExportableFactory; class User extends Model { use ExportableFactory; // ... }
- Export factory-generated data:
// Export to Excel $users = User::factory(100)->create()->toExcel(); // Export to CSV $users = User::factory(100)->create()->toCsv();
- Export Eloquent collections:
$users = User::whereNotNull('email_verified_at')->get(); $users->toExcel(); $users->toCsv();
Advanced Usage
Custom Filenames
You can specify custom filenames for your exports:
$users = User::factory(100)->create(); $users->toExcel('custom_users.xlsx'); $users->toCsv('custom_users.csv');
Column Selection and Renaming
The package provides a powerful pluck
method that allows you to select and rename columns:
$users = User::factory(100)->create(); // Select a single column $users->pluck('name')->toExcel(); // Select multiple columns $users->pluck(['name', 'email'])->toExcel(); // Select and rename columns $users->pluck([ 'name' => 'Full Name', 'email' => 'Email Address', 'created_at' => 'Registration Date' ])->toExcel();
You can also use Laravel's select
method to specify which columns to export:
// Select specific columns and export to CSV $users = User::factory(5)->create() ->select(['full_name', 'email']) ->toCsv('users-name-email.csv'); // Select specific columns and export to Excel $users = User::factory(5)->create() ->select(['full_name', 'email']) ->toExcel('users-name-email.xlsx');
Custom Column Selection for Excel
You can specify which columns to export to Excel:
$users = User::factory(100)->create(); $users->toExcel(null, ['name', 'email', 'created_at']);
File Locations
- CSV files are stored in
database/dumps/csv/
- Excel files are stored in
storage/app/dumps/excel/
(orworkbench/database/dumps/excel/
during testing)
The default filename is based on the model's table name (e.g., users.xlsx
or users.csv
). For non-model data, it defaults to export.xlsx
or export.csv
.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.