|
10 | 10 | use Illuminate\Contracts\Queue\ShouldBeUnique;
|
11 | 11 | use Illuminate\Contracts\Queue\ShouldQueue;
|
12 | 12 | use Illuminate\Contracts\Support\Arrayable;
|
| 13 | +use Illuminate\Database\Eloquent\Builder; |
| 14 | +use Illuminate\Database\Eloquent\Model; |
13 | 15 | use Illuminate\Foundation\Bus\Dispatchable;
|
14 | 16 | use Illuminate\Http\File;
|
15 | 17 | use Illuminate\Queue\InteractsWithQueue;
|
@@ -112,19 +114,29 @@ public function handle(): void
|
112 | 114 |
|
113 | 115 | $writer->addRow(Row::fromValues($headers));
|
114 | 116 |
|
| 117 | + $query = $dataTable->getFilteredQuery(); |
| 118 | + |
| 119 | + if ($query instanceof Builder) { |
| 120 | + $eagerLoads = array_keys($query->getEagerLoads()); |
| 121 | + } |
| 122 | + |
115 | 123 | if ($this->usesLazyMethod()) {
|
116 | 124 | $chunkSize = 1_000;
|
117 | 125 | if (is_int(config('datatables-export.chunk'))) {
|
118 | 126 | $chunkSize = config('datatables-export.chunk');
|
119 | 127 | }
|
120 |
| - $query = $dataTable->getFilteredQuery()->lazy($chunkSize); |
| 128 | + $results = $query->lazy($chunkSize); |
121 | 129 | } else {
|
122 |
| - $query = $dataTable->getFilteredQuery()->cursor(); |
| 130 | + $results = $query->cursor(); |
123 | 131 | }
|
124 | 132 |
|
125 |
| - foreach ($query as $row) { |
| 133 | + foreach ($results as $row) { |
126 | 134 | $cells = [];
|
127 | 135 |
|
| 136 | + if ($row instanceof Model && ! empty($eagerLoads)) { |
| 137 | + $row->loadMissing($eagerLoads); |
| 138 | + } |
| 139 | + |
128 | 140 | $row = $row instanceof Arrayable ? $row->toArray() : (array) $row;
|
129 | 141 |
|
130 | 142 | $defaultDateFormat = 'yyyy-mm-dd';
|
|
0 commit comments