Skip to content

Commit ba121a1

Browse files
committed
fix: Eager loads relation on non lazy mode
1 parent d757930 commit ba121a1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Jobs/DataTableExportJob.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Queue\ShouldBeUnique;
1111
use Illuminate\Contracts\Queue\ShouldQueue;
1212
use Illuminate\Contracts\Support\Arrayable;
13+
use Illuminate\Database\Eloquent\Model;
1314
use Illuminate\Foundation\Bus\Dispatchable;
1415
use Illuminate\Http\File;
1516
use Illuminate\Queue\InteractsWithQueue;
@@ -112,19 +113,26 @@ public function handle(): void
112113

113114
$writer->addRow(Row::fromValues($headers));
114115

116+
$query = $dataTable->getFilteredQuery();
117+
115118
if ($this->usesLazyMethod()) {
116119
$chunkSize = 1_000;
117120
if (is_int(config('datatables-export.chunk'))) {
118121
$chunkSize = config('datatables-export.chunk');
119122
}
120-
$query = $dataTable->getFilteredQuery()->lazy($chunkSize);
123+
$results = $query->lazy($chunkSize);
121124
} else {
122-
$query = $dataTable->getFilteredQuery()->cursor();
125+
$eagerLoads = array_keys($query->getEagerLoads());
126+
$results = $query->cursor();
123127
}
124128

125-
foreach ($query as $row) {
129+
foreach ($results as $row) {
126130
$cells = [];
127131

132+
if ($row instanceof Model && ! empty($eagerLoads)) {
133+
$row->loadMissing($eagerLoads);
134+
}
135+
128136
$row = $row instanceof Arrayable ? $row->toArray() : (array) $row;
129137

130138
$defaultDateFormat = 'yyyy-mm-dd';

0 commit comments

Comments
 (0)