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\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();
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');