- How do I export a Laravel Eloquent query to Excel with automatic chunking?
- Use the `FromQuery` export class and pass your Eloquent query builder instance. The package handles chunking automatically to optimize memory usage. For example: `return Excel::download(new UsersExport($query));`. Ensure your query uses `cursor()` if needed for very large datasets.
- Can I validate imported Excel data before saving to the database?
- Yes, use the `WithValidation` trait in your import class. Define validation rules in the `rules()` method, and the package will halt processing on failures. Failed rows trigger `FailedImport` events for logging or alerts. Example: `use WithValidation; public function rules(): array { return ['email' => 'required|email']; }`
- What Laravel versions does maatwebsite/excel support, and how do I check compatibility?
- The package officially supports Laravel 10+ (v3.1.x). For Laravel 8/9, use v3.1.x with PHP 8.0+. Check the [release notes](https://github.com/SpartnerNL/Laravel-Excel/releases) for version-specific requirements. Run `composer require maatwebsite/excel` and verify your `laravel/framework` version in `composer.json`.
- How do I handle large Excel imports (e.g., 500K+ rows) without memory issues?
- Use the `WithChunkReading` trait to process rows in chunks (e.g., 1000 at a time). For background processing, dispatch the import as a queued job: `Excel::queue(new LargeImport, $file, 'high')->onQueue('imports')`. Ensure your queue worker has sufficient memory (e.g., `queue:work --memory=512M`).
- Is there a way to customize Excel sheet formatting (e.g., colors, fonts, formulas) in exports?
- Yes, use the `WithStyles` or `WithFormatData` traits. For example, apply bold headers with `public function headings(): array { return ['Name', 'Email']; }` and `public function styles(Sheet $sheet) { $sheet->getStyle('A1:B1')->applyFromArray(['font' => ['bold' => true]]); }`. For formulas, use `WithFormatData` and the `setValue()` method.
- How do I test Excel exports/imports in Laravel’s PHPUnit?
- Use the package’s built-in assertions like `assertExported()` or `assertImported()`. Mock file uploads with `Storage::fake()` and `UploadedFile::fake()`. Example: `$this->assertExported($export, function (Array $rows) { return count($rows) === 10; });`. For imports, verify database changes with `assertDatabaseHas()`.
- Can I export Excel files directly to cloud storage (e.g., S3) instead of local disk?
- Yes, leverage Laravel’s filesystem configuration. Store the file path in a temporary location, then move it to S3 using `Storage::disk('s3')->put()`. Example: `Excel::store(new UsersExport, 'exports/users.xlsx', 's3');`. Ensure your `config/filesystems.php` is properly configured for S3.
- What are the alternatives to maatwebsite/excel for Laravel Excel handling?
- Alternatives include **Box/Spout** (lighter, CSV-focused) and **RaphaelStolt/MaatwebsiteExcel** (fork with additional features). However, **maatwebsite/excel** stands out for its deep Laravel integration (Eloquent, Queues, Events) and PhpSpreadsheet’s robust Excel support. For CSV-only needs, Spout may suffice, but it lacks Excel-specific features like formulas or multi-sheet exports.
- How do I log or alert on failed Excel import rows?
- Listen to the `FailedImport` event in your `EventServiceProvider`. Example: `protected $listen = [FailedImport::class => [ImportFailedListener::class],];`. In the listener, log failed rows to a database table or send alerts via Laravel Notifications. Access failed rows via `$event->failures()`.
- Does maatwebsite/excel support dynamic Excel generation from Blade views?
- Yes, use the `FromView` export class to render Blade templates as Excel files. Example: `return Excel::download(new UserViewExport, 'users.xlsx');`. Define your view with `public function view(): string { return view('exports.users'); }`. This is useful for generating reports with dynamic data and styling.