Skip to content

Commit c75ac6a

Browse files
Support exporting relation queries (SpartnerNL#2089)
1 parent 7c0ae64 commit c75ac6a

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

src/Jobs/AppendQueryToSheet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maatwebsite\Excel\Jobs;
44

55
use Illuminate\Bus\Queueable;
6+
use Maatwebsite\Excel\Cell;
67
use Maatwebsite\Excel\Writer;
78
use Maatwebsite\Excel\Files\TemporaryFile;
89
use Illuminate\Contracts\Queue\ShouldQueue;

src/Jobs/SerializedQuery.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maatwebsite\Excel\Jobs;
44

55
use Closure;
6+
use Illuminate\Database\Eloquent\Relations\Relation;
67
use Illuminate\Support\Facades\DB;
78
use Opis\Closure\SerializableClosure;
89
use Illuminate\Database\Query\Builder;
@@ -37,7 +38,7 @@ class SerializedQuery
3738
public $with = [];
3839

3940
/**
40-
* @param Builder|\Illuminate\Database\Eloquent\Builder $builder
41+
* @param Builder|Relation|EloquentBuilder $builder
4142
*/
4243
public function __construct($builder)
4344
{
@@ -46,7 +47,7 @@ public function __construct($builder)
4647
$this->connection = $builder->getConnection()->getName();
4748
$this->with = $this->serializeEagerLoads($builder);
4849

49-
if ($builder instanceof EloquentBuilder) {
50+
if ($builder instanceof EloquentBuilder || $builder instanceof Relation) {
5051
$this->model = get_class($builder->getModel());
5152
}
5253
}

tests/Concerns/FromQueryTest.php

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

33
namespace Maatwebsite\Excel\Tests\Concerns;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Facades\DB;
7+
use Maatwebsite\Excel\Tests\Data\Stubs\FromGroupUsersQueuedQueryExport;
68
use Maatwebsite\Excel\Tests\TestCase;
79
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
810
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group;
@@ -64,6 +66,24 @@ public function can_export_from_query()
6466
$this->assertEquals($allUsers, $contents);
6567
}
6668

69+
/**
70+
* @test
71+
*/
72+
public function can_export_from_relation_query_queued()
73+
{
74+
$export = new FromGroupUsersQueuedQueryExport();
75+
76+
$export->queue('from-query-store.xlsx');
77+
78+
$contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-query-store.xlsx', 'Xlsx');
79+
80+
$allUsers = $export->query()->get()->map(function($row) use ($export) {
81+
return $export->map($row);
82+
})->toArray();
83+
84+
$this->assertEquals($allUsers, $contents);
85+
}
86+
6787
/**
6888
* @test
6989
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Maatwebsite\Excel\Tests\Data\Stubs;
4+
5+
use Illuminate\Database\Query\Builder;
6+
use Maatwebsite\Excel\Concerns\FromQuery;
7+
use Maatwebsite\Excel\Concerns\Exportable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
10+
use Maatwebsite\Excel\Concerns\WithMapping;
11+
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group;
12+
13+
class FromGroupUsersQueuedQueryExport implements FromQuery, WithCustomChunkSize, ShouldQueue, WithMapping
14+
{
15+
use Exportable;
16+
17+
/**
18+
* @return Builder
19+
*/
20+
public function query()
21+
{
22+
return Group::first()->users();
23+
}
24+
25+
/**
26+
* @param mixed $row
27+
*
28+
* @return array
29+
*/
30+
public function map($row): array
31+
{
32+
return [
33+
$row->name,
34+
$row->email,
35+
];
36+
}
37+
38+
/**
39+
* @return int
40+
*/
41+
public function chunkSize(): int
42+
{
43+
return 10;
44+
}
45+
}

0 commit comments

Comments
 (0)