PHP code example of phpmystic / eloquent-exporter

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

    

phpmystic / eloquent-exporter example snippets


use PhpMystic\EloquentExporter\EloquentExporter;

// Export all users to CSV
EloquentExporter::for(User::class)
    ->columns(['name', 'email', 'created_at'])
    ->toCsv(storage_path('app/exports/users.csv'));

// Export all users to Excel
EloquentExporter::for(User::class)
    ->columns(['name', 'email', 'created_at'])
    ->toExcel(storage_path('app/exports/users.xlsx'));

EloquentExporter::query(User::where('is_active', true)->orderBy('name'))
    ->columns(['name', 'email'])
    ->toExcel('active-users.xlsx');

EloquentExporter::for(User::class)
    ->columns([
        'name' => 'Full Name',
        'email' => 'Email Address',
        'created_at' => 'Registered At',
    ])
    ->toCsv('users.csv');

EloquentExporter::for(User::class)
    ->columns([
        'name',
        'email',
        'department.name' => 'Department',
        'department.manager.name' => 'Manager',
    ])
    ->toCsv('users.csv');

EloquentExporter::for(User::class)
    ->columns([
        'name',
        'roles.name' => 'Roles',
    ])
    ->toCsv('users.csv');

EloquentExporter::for(User::class)
    ->columns([
        'name',
        'tags.label' => 'Tags',
    ])
    ->separator('tags.label', ' | ')
    ->toCsv('users.csv');

EloquentExporter::for(Order::class)
    ->columns([
        'id' => 'Order #',
        'customer.name' => 'Customer',
        'items.product.name' => 'Product',
        'items.quantity' => 'Qty',
        'items.price' => 'Price',
    ])
    ->expandRows('items')
    ->toExcel('orders.xlsx');

EloquentExporter::for(Company::class)
    ->columns([
        'name' => 'Company',
        'departments.name' => 'Department',
        'departments.employees.name' => 'Employee',
        'departments.employees.email' => 'Email',
    ])
    ->expandRows('departments.employees')
    ->toCsv('company-roster.csv');

EloquentExporter::for(User::class)
    ->columns([
        'name',
        'projects.name' => 'Project',
        'projects.pivot.role' => 'Role',
        'projects.pivot.joined_at' => 'Joined',
    ])
    ->expandRows('projects')
    ->toExcel('team.xlsx');

EloquentExporter::for(Order::class)
    ->columns([
        'id' => 'Order #',
        'total' => 'Total',
        'created_at' => 'Date',
    ])
    ->format('total', fn ($value) => number_format($value, 2) . ' USD')
    ->format('created_at', fn ($value) => $value->format('Y-m-d'))
    ->toExcel('orders.xlsx');

public function export()
{
    return EloquentExporter::for(User::class)
        ->columns(['name', 'email'])
        ->download('users.xlsx'); // or 'users.csv'
}

EloquentExporter::for(User::class)
    ->columns(['name', 'email'])
    ->chunk(1000)
    ->toCsv('all-users.csv');

use PhpMystic\EloquentExporter\Exportable;

class User extends Model
{
    use Exportable;
}

// From the model
User::exporter()
    ->columns(['name', 'email'])
    ->toCsv('users.csv');

// From a query builder
User::where('is_active', true)
    ->exporter()
    ->columns(['name', 'email'])
    ->download('active-users.xlsx');

EloquentExporter::for(User::class)
    ->columns(['name', 'email'])
    ->sheetName('Users')
    ->toExcel('users.xlsx');

EloquentExporter::query(User::where('is_active', true))
    ->columns(['name', 'email'])
    ->sheetName('Active Users')
    ->addSheet(
        User::where('is_active', false),
        ['name', 'email'],
        'Inactive Users'
    )
    ->toExcel('users.xlsx');

EloquentExporter::for(User::class)
    ->sheetPerRecord('name')
    ->columns([
        'posts.title' => 'Title',
        'posts.status' => 'Status',
        'posts.views' => 'Views',
    ])
    ->toExcel('user-posts.xlsx');

EloquentExporter::for(User::class)
    ->sheetPerRecord(fn (User $user) => $user->name . ' (' . $user->email . ')')
    ->columns([
        'posts.title' => 'Title',
        'posts.status' => 'Status',
    ])
    ->toExcel('user-posts.xlsx');

EloquentExporter::for(User::class)
    ->sheetPerRecord('name')
    ->sheetMeta([
        'email' => 'Email',
        'department.name' => 'Department',
    ])
    ->columns([
        'posts.title' => 'Title',
        'posts.status' => 'Status',
    ])
    ->toExcel('user-posts.xlsx');

EloquentExporter::for(User::class)
    ->columns(['name', 'email'])
    ->sheetName('Summary')
    ->addDynamicSheets(
        User::has('posts'),
        fn (User $user) => $user->name,
        ['posts.title' => 'Title', 'posts.status' => 'Status']
    )
    ->toExcel('full-report.xlsx');