Skip to content

Commit 7f3f5d1

Browse files
Add implicit queued exports
1 parent 43ea772 commit 7f3f5d1

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

docs/export/queued.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ return (new InvoicesExport)->queue('invoices.xlsx');
2727
Behind the scenes the query will be chunked and multiple jobs will be chained. These jobs will be executed in the correct order,
2828
and will only execute if non of the previous have failed.
2929

30+
### Implicit Export queueing
31+
32+
You can also mark an export implicitly as a queued export. You can do this by using Laravel's `ShouldQueue` contract.
33+
34+
```php
35+
namespace App\Exports;
36+
37+
class InvoicesExport implements FromQuery, ShouldQueue
38+
{
39+
use Exportable;
40+
41+
public function query()
42+
{
43+
return Invoice::query();
44+
}
45+
}
46+
```
47+
48+
In your controller you can now call the normal `->store()` method.
49+
Based on the presence of the `ShouldQueue` contract, the export will be queued.
50+
51+
```php
52+
return (new InvoicesExport)->store('invoices.xlsx');
53+
```
54+
3055
### Appending jobs
3156

3257
The `queue()` method returns an instance of Laravel's `PendingDispatch`. This means you can chain extra jobs.

src/Concerns/Exportable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function download(string $fileName = null, string $writerType = null)
3232
* @param string|null $writerType
3333
*
3434
* @throws InvalidArgumentException
35-
* @return bool
35+
* @return bool|PendingDispatch
3636
*/
3737
public function store(string $filePath = null, string $disk = null, string $writerType = null)
3838
{

src/Excel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maatwebsite\Excel;
44

5+
use Illuminate\Contracts\Queue\ShouldQueue;
56
use Illuminate\Filesystem\FilesystemManager;
67
use Illuminate\Contracts\Routing\ResponseFactory;
78

@@ -82,6 +83,10 @@ public function download($export, string $fileName, string $writerType = null)
8283
*/
8384
public function store($export, string $filePath, string $disk = null, string $writerType = null)
8485
{
86+
if ($export instanceof ShouldQueue) {
87+
return $this->queue($export, $filePath, $disk, $writerType);
88+
}
89+
8590
$file = $this->export($export, $filePath, $writerType);
8691

8792
return $this->filesystem->disk($disk)->put($filePath, fopen($file, 'r+'));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Maatwebsite\Excel\Tests\Data\Stubs;
4+
5+
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Maatwebsite\Excel\Concerns\Exportable;
7+
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
8+
9+
class ShouldQueueExport implements WithMultipleSheets, ShouldQueue
10+
{
11+
use Exportable;
12+
13+
/**
14+
* @return SheetWith100Rows[]
15+
*/
16+
public function sheets(): array
17+
{
18+
return [
19+
new SheetWith100Rows('A'),
20+
new SheetWith100Rows('B'),
21+
new SheetWith100Rows('C'),
22+
];
23+
}
24+
}

tests/QueuedExportTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExport;
66
use Maatwebsite\Excel\Tests\Data\Stubs\AfterQueueExportJob;
7+
use Maatwebsite\Excel\Tests\Data\Stubs\ShouldQueueExport;
78

89
class QueuedExportTest extends TestCase
910
{
@@ -30,4 +31,16 @@ public function can_queue_an_export_and_store_on_different_disk()
3031
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
3132
]);
3233
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function can_implicitly_queue_an_export()
39+
{
40+
$export = new ShouldQueueExport();
41+
42+
$export->store('queued-export.xlsx', 'test')->chain([
43+
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
44+
]);
45+
}
3346
}

0 commit comments

Comments
 (0)