petersowah/laravel-factory-dumps

Export Laravel factory data to CSV or Excel files

0.0.4 2025-03-21 09:26 UTC

This package is auto-updated.

Last update: 2025-03-21 13:14:20 UTC


README

Social Card of Laravel Activity Log

Easily export your Eloquent models to Excel and CSV formats.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

  1. Import the ExportableFactory trait in your model:
use PeterSowah\LaravelFactoryDumps\Traits\ExportableFactory;

class User extends Model
{
    use ExportableFactory;
    // ...
}
  1. Export factory-generated data:
// Export to Excel
$users = User::factory(100)->create()->toExcel();

// Export to CSV
$users = User::factory(100)->create()->toCsv();
  1. 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/ (or workbench/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.