I’m facing an issue with Laravel Excel where the export functionality creates an empty file on disk. Below is my code for importing and exporting Excel files. The import process works fine, but the exported file export.xlsx is empty. Can anyone help me identify what might be going wrong?
namespace AppHttpControllers;
use IlluminateHttpRequest;
use YajraDatatablesDatatables;
use IlluminateSupportFacadesStorage;
use AppModelsRrUploadExcel;
use AppImportsRrUploadExcelImport;
use IlluminateSupportFacadesFile;
use MaatwebsiteExcelFacadesExcel;
use IlluminateSupportFacadesHttp;
use AppExportsRrUploadExcelExport;
use Validator;
use MyCLabsEnumEnum;
use ZipStreamZipStream;
use ZipStreamOperationModeMode;
use AppExportsRrUploadExcelExport;
class RrUploadExcelController extends Controller
{
public function index(Request $request)
{
$outcome = "";
$message = "Message index";
$import = '';
if ($request->hasFile('upload_excel')) {
// Save import details to the database
$rr_upload_excel = new RrUploadExcel();
$outcome = "";
$fileName = date('Y-m-d-H-i').'-'.$request->file('upload_excel')->getClientOriginalName();
$request->file('upload_excel')->move(public_path('uploads_excel'), $fileName);
$directory = public_path('uploads_excel');
$files = File::files($directory);
$rr_upload_excel->file_name = $fileName;
$rr_upload_excel->customer_ref_code = 'admin';
$rr_upload_excel->outcome = $outcome;
$rr_upload_excel->timestamp = now();
$rr_upload_excel->save();
foreach ($files as $file) {
if ($file->getFilename() == $fileName) {
$RrUploadExcelImport = new RrUploadExcelImport($rr_upload_excel->upload_id);
Excel::import($RrUploadExcelImport, $file);
break; // Stop the loop once the correct file is found and processed
}
}
}
return view('rr_upload_excel.index', compact('outcome','message','import'));
}
}
This is the Import Class:
namespace AppImports;
use IlluminateSupportCollection;
use MaatwebsiteExcelConcernsToCollection;
use MaatwebsiteExcelConcernsWithHeadingRow;
use MaatwebsiteExcelConcernsImportable;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesValidator;
use IlluminateSupportFacadesHttp;
use AppModelsRrCart;
use AppModelsRrStock;
use AppModelsRrUploadExcel;
use AppExportsRrUploadExcelExport;
use MaatwebsiteExcelFacadesExcel;
class RrUploadExcelImport implements ToCollection, WithHeadingRow
{
use Importable;
protected $rrUploadExcelId;
protected $errorMessage;
public function __construct($rrUploadExcelId)
{
$this->rrUploadExcelId = $rrUploadExcelId;
$this->errorMessage = '';
}
public function collection(Collection $rows)
{
// CODE THAT MANAGE $ROWS and PUSH RESULTS IN DATA_ARR
...
$data_arr = [
['customer' => 'Client 1', 'destination' => 'Destination 1', 'shipping_date' => '2024-01-01', 'notes' => 'Note 1', 'code' => 'Code 1', 'quantity' => 10, 'outcome' => 'Success'],
['customer' => 'Client 2', 'destination' => 'Destination 2', 'shipping_date' => '2024-01-02', 'notes' => 'Note 2', 'code' => 'Code 2', 'quantity' => 20, 'outcome' => 'Failed'],
];
Excel::store(new RrUploadExcelExport($data_arr), 'rr_upload_outcome_Import.xlsx');
}
}
This is the Export Class:
namespace AppExports;
use IlluminateSupportCollection;
use MaatwebsiteExcelConcernsFromCollection;
use MaatwebsiteExcelConcernsWithHeadings;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;
use MaatwebsiteExcelConcernsFromArray;
use AppModelsUser;
class RrUploadExcelExport implements FromCollection, WithHeadings
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function collection()
{
return collect($this->data);
}
public function headings(): array
{
return [
'customer',
'destination',
'date',
'notes',
'code',
'quantity',
'outcome',
];
}
}
In addition to creating the empty file on the site, a file called index without any extension will automatically download.
Can anyone help me understand why the export functionality is producing an empty file?