PHP code example of hexagonlabsllc / laravel-exports

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

    

hexagonlabsllc / laravel-exports example snippets


use HexagonLabsLLC\LaravelExports\Models\{ExportModel, ExportLayout, ExportColumn};
use HexagonLabsLLC\LaravelExports\Services\DynamicExportService;

// 1. Create an export layout
$userModel = ExportModel::where('title', 'User')->first();
$layout = ExportLayout::create([
    'export_model_id' => $userModel->id,
    'title' => 'User Export',
]);

// 2. Define columns
ExportColumn::create([
    'export_layout_id' => $layout->id,
    'title' => 'Email',
    'value_path' => 'email',
    'position' => 1,
]);

ExportColumn::create([
    'export_layout_id' => $layout->id,
    'title' => 'Created',
    'value_path' => 'created_at',
    'position' => 2,
]);

// 3. Export data
$exportService = new DynamicExportService();
return $exportService->downloadAs($layout, 'csv', 'users.csv');

// Export customer name through multiple relationships
ExportColumn::create([
    'export_layout_id' => $layout->id,
    'title' => 'Customer Name',
    'value_path' => 'workItem.workOrder.customer.contact.org_name',
    'position' => 3,
]);

// Export user name through simple relationship
ExportColumn::create([
    'export_layout_id' => $layout->id,
    'title' => 'Assigned User',
    'value_path' => 'user.name',
    'position' => 4,
]);

// Create filters for specific identifier types
$containerFilter = ExportFilter::create([
    'export_layout_id' => $layout->id,
    'export_model_id' => $identifierModel->id,
    'export_model_relation_id' => $typeRelation->id, // Points to type.title
    'operator' => 'relation',
    'value' => 'Container',
]);

// Create column that shows only Container identifier values
ExportColumn::create([
    'export_layout_id' => $layout->id,
    'export_model_relation_id' => $identifiersRelation->id,
    'export_filter_id' => $containerFilter->id,
    'title' => 'Container ID',
    'value_path' => 'workItem.identifiers.value',
    'default' => '0', // Show 0 if no Container identifier found
    'position' => 5,
]);

$formatDate = ExportFunction::where('name', 'Format Date')->first();

ExportColumn::create([
    'export_layout_id' => $layout->id,
    'title' => 'Joined Date',
    'value_path' => 'created_at',
    'export_function_id' => $formatDate->id,
    'export_function_values' => json_encode(['F j, Y']), // January 1, 2025
    'position' => 6,
]);

ExportFilter::create([
    'export_layout_id' => $layout->id,
    'export_model_id' => $userModel->id,
    'operator' => 'between',
    'is_request' => true,
    'is_   'date_range' => ['2025-01-01', '2025-12-31']
]);

// Streaming export with chunking
return $exportService->streamAs(
    $layout,
    'csv',
    'large-export.csv',
    $requestData,
    ['delimiter' => ','],
    1000 // Chunk size
);

// Export with custom eager loading
$exportService = new DynamicExportService();
$exportService->loadLayout($layout);
// The service automatically optimizes eager loading based on your column configurations

// In your .env file
APP_DEBUG=true

// The service will automatically log:
// - Column processing details
// - Relation loading status
// - Query execution information
// - Collection filtering results
bash
# Publish configuration
php artisan vendor:publish --provider="HexagonLabsLLC\LaravelExports\LaravelExportsServiceProvider"

# Run migrations
php artisan migrate

# Import your models
php artisan export:import-models --sync-relations

# Seed transformation functions
php artisan export:seed-functions
bash
# First time seeding
php artisan export:seed-functions

# Update existing functions
php artisan export:seed-functions --force