PHP code example of nghia-kun / laravel-file-export

1. Go to this page and download the library: Download nghia-kun/laravel-file-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nghia-kun / laravel-file-export example snippets


use NghiaKun\FileExport\Export\BaseExport;

class UserExport extends BaseExport
{
    public function query()
    {
        return User::query();
    }

    public function headings(): array
    {
        return ['id', 'name', 'email'];
    }

    public function map($user): array
    {
        return [$user->id, $user->name, $user->email];
    }
}

use NghiaKun\FileExport\Export\ExportService;

// Xuất ra 1 file .tsv
(new ExportService(new UserExport(), storage_path('app/users.tsv'), 'tsv'))->handle();

// Xuất ra 1 file .zip chứa nhiều file .tsv (chia theo recordsPerFile)
(new ExportService(new UserExport(), storage_path('app/users.zip'), 'zip'))->handle();

use NghiaKun\FileExport\Compress\ZipService;

(new ZipService())->compressFolder($sourceDir, $zipDestination, true);

return [
    'chunk_size' => (int) env('EXPORT_CHUNK_SIZE', 1000),
    'primary_key' => env('EXPORT_PRIMARY_KEY', 'id'),
    'reading_strategy' => env('EXPORT_READING_STRATEGY', 'cursor'), // 'cursor' | 'chunkById'
    'count_total' => env('EXPORT_COUNT_TOTAL', true),
    'records_per_file' => (int) env('EXPORT_RECORDS_PER_FILE', 50000), // chỉ áp dụng khi export dạng zip
    'csv' => [
        'delimiter' => env('EXPORT_CSV_DELIMITER', ','),
        'enclosure' => env('EXPORT_CSV_ENCLOSURE', ''),
    ],
];
bash
php artisan make:export UserExport
bash
php artisan vendor:publish --tag=file-export-stubs
bash
php artisan vendor:publish --tag=file-export-config