1. Go to this page and download the library: Download osama-98/laravel-exports 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/ */
osama-98 / laravel-exports example snippets
namespace App\Exports;
use App\Models\User;
use Carbon\Carbon;
use Osama\LaravelExports\Exports\ExportColumn;
use Osama\LaravelExports\Exports\Exporter;
use Osama\LaravelExports\Exports\Models\Export;
class UserExporter extends Exporter
{
public static function getModel(): string
{
return User::class;
}
public static function getColumns(): array
{
return [
ExportColumn::make('id')
->label('ID'),
ExportColumn::make('name')
->label('Full Name'),
ExportColumn::make('email')
->label('Email Address'),
ExportColumn::make('created_at')
->label('Created At')
->formatStateUsing(fn ($state) => $state ? Carbon::parse($state)->format('Y-m-d H:i:s') : null),
];
}
public static function getCompletedNotificationBody(Export $export): string
{
return "Your user export has completed with {$export->successful_rows} rows exported.";
}
}
use App\Exports\UserExporter;
use App\Models\User;
use Osama\LaravelExports\Facades\Export as ExportFacade;
use Osama\LaravelExports\Exports\Enums\ExportFormat;
// Basic export to XLSX
$export = ExportFacade::exporter(UserExporter::class)
->formats([ExportFormat::Xlsx])
->start(User::query(), creator: auth()->user());
// Export to both CSV and XLSX
$export = ExportFacade::exporter(UserExporter::class)
->formats([ExportFormat::Csv, ExportFormat::Xlsx])
->chunkSize(500)
->start(User::query(), creator: auth()->user());
use App\Exports\UserExporter;
use App\Models\User;
use Osama\LaravelExports\Exports\ExportManager;
use Osama\LaravelExports\Exports\Enums\ExportFormat;
public function exportUsers(ExportManager $exportManager)
{
$export = $exportManager
->exporter(UserExporter::class)
->chunkSize(500)
->formats([ExportFormat::Csv, ExportFormat::Xlsx])
->start(User::query(), creator: auth()->user());
return response()->json([
'export_id' => $export->id,
'status' => $export->status->value,
'total_rows' => $export->total_rows,
]);
}
use Osama\LaravelExports\Exports\Models\Export;
use Osama\LaravelExports\Exports\Enums\ExportStatus;
$export = Export::find($exportId);
// Check status
if ($export->status === ExportStatus::Completed) {
// Export is ready
}
// Get progress percentage
$progress = $export->progressPercentage();
use Osama\LaravelExports\Exports\Models\Export;
use Osama\LaravelExports\Exports\Enums\ExportStatus;
public function downloadExport(Export $export, string $format = 'xlsx')
{
if ($export->status !== ExportStatus::Completed) {
abort(400, 'Export is not completed yet');
}
return $export->download($format);
}
$export = ExportFacade::exporter(UserExporter::class)
->modifyQueryUsing(function ($query, $options) {
// Filter users created in the last 30 days
return $query->where('created_at', '>=', now()->subDays(30));
})
->formats([ExportFormat::Csv])
->start(User::query(), creator: auth()->user());
use Illuminate\Support\Facades\DB;
$export = ExportFacade::exporter(UserExporter::class)
->formats([ExportFormat::Csv])
->start(DB::table('users')->where('active', true), creator: auth()->user());
$export = ExportFacade::exporter(UserExporter::class)
->fileDisk('s3') // Make sure 's3' disk is configured in config/filesystems.php
->formats([ExportFormat::Xlsx])
->start(User::query()->limit(1000), creator: auth()->user());
ExportColumn::make('full_name')
->label('Full Name')
->getStateUsing(function ($user) {
return "{$user->first_name} {$user->last_name}";
});
// Use dot notation for relationships
ExportColumn::make('profile.bio')
->label('Biography');
// Use default value
ExportColumn::make('status')
->default('active');
ExportColumn::make('orders_count')
->label('Total Orders')
->counts('orders');
ExportColumn::make('orders_avg_amount')
->label('Average Order Amount')
->avg('orders', 'amount');
ExportColumn::make('orders_sum_amount')
->label('Total Order Amount')
->sum('orders', 'amount');
ExportColumn::make('has_orders')
->label('Has Orders')
->exists('orders');
class UserExporter extends Exporter
{
public static function modifyQuery($query)
{
return $query->where('active', true)
->with('profile');
}
}
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\CellAlignment;
class UserExporter extends Exporter
{
public function getXlsxCellStyle(): ?Style
{
return (new Style())
->setFontSize(12)
->setFontColor(Color::BLACK);
}
public function getXlsxHeaderCellStyle(): ?Style
{
return (new Style())
->setFontSize(14)
->setFontBold()
->setFontColor(Color::WHITE)
->setBackgroundColor(Color::BLUE)
->setCellAlignment(CellAlignment::CENTER);
}
}
// In your EventServiceProvider
use Osama\LaravelExports\Exports\Events\ExportCompleted;
use Illuminate\Support\Facades\Event;
Event::listen(ExportCompleted::class, function (ExportCompleted $event) {
$export = $event->export;
// Send notification to user
if ($export->creator) {
$export->creator->notify(
new ExportReadyNotification($export)
);
}
// Or log the completion
Log::info("Export {$export->id} completed with {$export->successful_rows} rows");
});
namespace App\Listeners;
use Osama\LaravelExports\Exports\Events\ExportCompleted;
use Illuminate\Support\Facades\Notification;
class SendExportCompletionNotification
{
public function handle(ExportCompleted $event): void
{
$export = $event->export;
if ($export->creator) {
$export->creator->notify(
new ExportReadyNotification($export)
);
}
}
}