PHP code example of fersot / model-export

1. Go to this page and download the library: Download fersot/model-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/ */

    

fersot / model-export example snippets


use Fersot\ModelExport\Exporter;

// Download directly from a controller
return Exporter::from(User::query())->download('users.xlsx');

// Save to disk
Exporter::from(Order::where('status', 'paid'))->toExcel(storage_path('exports/orders.xlsx'));

// With column mapping
return Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo'),
        ExportColumn::make('created_at')->label('Registro')->transform(fn($v) => $v->format('d/m/Y')),
    ])
    ->download('usuarios.xlsx');

use Fersot\ModelExport\Exporter;

// Eloquent Builder
Exporter::from(User::where('active', 1));

// Illuminate Collection
Exporter::from(collect([['name' => 'Alice'], ['name' => 'Bob']]));

// Plain PHP array
Exporter::from([['product' => 'Widget', 'price' => 9.99]]);

// Model shortcut (uses Model::query())
Exporter::model(User::class);

use Fersot\ModelExport\ExportColumn;

Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo Electrónico'),
        ExportColumn::make('age')->label('Edad')->transform(fn($v) => "{$v} años"),

        // The transform also receives the full row as a second argument
        ExportColumn::make('name')->label('Info')
            ->transform(fn($v, $row) => "{$v} <{$row['email']}>"),
    ])
    ->download('users.xlsx');

// Keep only these columns
Exporter::from(User::query())->only(['name', 'email'])->toArray();

// Exclude columns
Exporter::from(User::query())->except(['password', 'remember_token'])->toArray();

Exporter::from(User::query())
    ->chunk(500)           // default: 1000
    ->download('all_users.xlsx');

$exporter = Exporter::from(User::query());

// PHP array / Collection
$exporter->toArray();
$exporter->toCollection();

// Save to file
$exporter->toExcel('path/to/output.xlsx');   // returns the path
$exporter->toCsv('path/to/output.csv');      // returns the path

// HTTP download (StreamedResponse)
$exporter->download('filename.xlsx');
$exporter->downloadCsv('filename.csv');

// Builder macros
User::where('active', 1)->toExcel(storage_path('exports/active_users.xlsx'));
User::where('active', 1)->toCsv(storage_path('exports/active_users.csv'));

// Return a download response directly
return User::query()->exportDownload('users.xlsx');
return User::query()->exportDownloadCsv('users.csv');

// Collection macros
$collection->toExcel('path/to/file.xlsx');
return $collection->exportDownload('data.xlsx');

use Fersot\ModelExport\Traits\HasExport;

class User extends Model
{
    use HasExport;
}

// Export all records
return User::export()->only(['name', 'email'])->download('users.xlsx');

// Export with a scoped query
return User::exportQuery(fn($q) => $q->where('role', 'admin'))
    ->download('admins.xlsx');

return [
    'chunk_size'     => 1000,
    'disk'           => 'local',
    'default_format' => 'xlsx',
    'temp_path'      => null,   // null = sys_get_temp_dir()
];
bash
php artisan vendor:publish --tag=model-export-config