Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Model Export Laravel Package

php-dominicana/laravel-model-export

View on GitHub
Deep Wiki
Context7

Getting Started

To quickly leverage the new PDF export feature, install the package via Composer:

composer require vendor/package-name

Then, publish the package's configuration (if applicable) and assets:

php artisan vendor:publish --provider="Vendor\PackageName\PackageServiceProvider" --tag="config"
php artisan vendor:publish --provider="Vendor\PackageName\PackageServiceProvider" --tag="assets"

For your first use case, generate a PDF from an existing Eloquent model or collection:

use Vendor\PackageName\Facades\PackageName;

$pdf = PackageName::query(Model::class)
    ->where('active', true)
    ->exportToPdf();

The package will automatically handle view rendering and PDF generation using the default template (located at resources/views/vendor/package-name/pdf/default.blade.php).


Implementation Patterns

Core Workflow

  1. Query Integration: Chain the exportToPdf() method onto an Eloquent query builder instance:
    $pdf = Model::where('status', 'published')->exportToPdf();
    
  2. Customization: Override the default PDF template by publishing the asset and modifying:
    PackageName::exportToPdf()->setTemplate('custom.template');
    
  3. Dynamic Data: Pass additional data to the PDF view:
    PackageName::exportToPdf()->with(['custom_data' => 'value']);
    
  4. Download/Display: Use the returned PDF object to:
    • Force download: $pdf->download('filename.pdf')
    • Display inline: $pdf->stream('filename.pdf')
    • Save to storage: $pdf->save(storage_path('app/filename.pdf'))

Advanced Patterns

  • Batch Processing: Export large datasets with pagination:
    $pdf = Model::paginate(50)->exportToPdf();
    
  • Event Hooks: Listen for package.pdf.exporting and package.pdf.exported events to modify behavior:
    event(new ExportingEvent($query, $options));
    
  • Service Integration: Integrate with Laravel's queue system for async generation:
    dispatch(new ExportPdfJob(Model::class, $options));
    

Gotchas and Tips

Common Pitfalls

  1. Template Paths: Ensure custom templates are placed in resources/views/vendor/package-name/ or the package will fall back to the default.
  2. Memory Limits: Large datasets may hit PHP's memory limit. Use chunking or queue jobs for exports >1000 records.
  3. CSS Conflicts: PDF rendering may ignore some CSS properties (e.g., position: fixed). Test styles in the default template first.

Debugging Tips

  • Log Exports: Enable debug mode to log PDF generation:
    config(['package-name.debug' => true]);
    
  • View Inspection: Use dd($pdf->getViewData()) to inspect data passed to the template before rendering.
  • Error Handling: Wrap exports in try-catch blocks to handle potential exceptions:
    try {
        $pdf = Model::exportToPdf();
    } catch (\Vendor\PackageName\Exceptions\ExportException $e) {
        Log::error($e->getMessage());
    }
    

Extension Points

  1. Custom PDF Engines: Override the default PDF engine (e.g., switch from DomPDF to Snappy):
    PackageName::setPdfEngine(new \Snappy());
    
  2. Dynamic Filenames: Use a closure to generate filenames dynamically:
    PackageName::exportToPdf()->setFilename(fn() => 'export_'.now()->format('Y-m-d').'.pdf');
    
  3. Post-Export Actions: Chain additional actions after export:
    $pdf->exportToPdf()->then(function($pdf) {
        // Send email with PDF attachment
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle