承接 petersowah/laravel-factory-dumps 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

petersowah/laravel-factory-dumps

最新稳定版本:0.0.4

Composer 安装命令:

composer require petersowah/laravel-factory-dumps

包简介

Export Laravel factory data to CSV or Excel files

README 文档

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.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-28