Skip to content

Commit eafb4ab

Browse files
committed
Fix issue when exports folder doesn't exist
1 parent e3aa9ad commit eafb4ab

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Jobs/DataTableExportJob.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Queue\SerializesModels;
1717
use Illuminate\Support\Arr;
1818
use Illuminate\Support\Facades\Auth;
19+
use Illuminate\Support\Facades\File;
1920
use Illuminate\Support\Str;
2021
use PhpOffice\PhpSpreadsheet\Shared\Date;
2122
use Yajra\DataTables\Html\Column;
@@ -53,6 +54,9 @@ public function __construct(array $dataTable, array $request, $user = null)
5354
* Execute the job.
5455
*
5556
* @return void
57+
* @throws \Box\Spout\Common\Exception\IOException
58+
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
59+
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
5660
*/
5761
public function handle()
5862
{
@@ -70,14 +74,20 @@ public function handle()
7074
$dataTable = app()->call([$oTable, 'dataTable'], compact('query'));
7175
$dataTable->skipPaging();
7276

77+
$exportPath = storage_path('app/exports');
78+
79+
if (! File::isDirectory($exportPath)) {
80+
File::makeDirectory($exportPath);
81+
}
82+
7383
$type = Str::startsWith(request('exportType'), Type::CSV) ? Type::CSV : Type::XLSX;
7484
$writer = WriterEntityFactory::createWriter($type);
75-
$writer->openToFile(storage_path('app/exports/' . $this->batchId . '.' . $type));
85+
$writer->openToFile($exportPath.'/'.$this->batchId.'.'.$type);
7686

7787
$columns = $oTable->html()->getColumns()->filter->exportable;
7888
$writer->addRow(
7989
WriterEntityFactory::createRowFromArray(
80-
$columns->map(fn($column) => strip_tags($column['title']))->toArray()
90+
$columns->map(fn ($column) => strip_tags($column['title']))->toArray()
8191
)
8292
);
8393

@@ -123,7 +133,7 @@ public function handle()
123133
*/
124134
protected function wantsDateFormat(Column $column): bool
125135
{
126-
if (!isset($column['exportFormat'])) {
136+
if (! isset($column['exportFormat'])) {
127137
return false;
128138
}
129139

0 commit comments

Comments
 (0)