Код: Выделить всё
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Facades\Storage;
use App\Models\RrUploadExcel;
use App\Imports\RrUploadExcelImport;
use Illuminate\Support\Facades\File;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Http;
use App\Exports\RrUploadExcelExport;
use Validator;
use MyCLabs\Enum\Enum;
use ZipStream\ZipStream;
use ZipStream\OperationMode\Mode;
use App\Exports\RrUploadExcelExport;
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'));
}
}
Код: Выделить всё
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\Importable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Http;
use App\Models\RrCart;
use App\Models\RrStock;
use App\Models\RrUploadExcel;
use App\Exports\RrUploadExcelExport;
use Maatwebsite\Excel\Facades\Excel;
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');
}
}
Код: Выделить всё
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Maatwebsite\Excel\Concerns\FromArray;
use App\Models\User;
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',
];
}
}
Может ли кто-нибудь помочь мне понять, почему функция экспорта создает пустой файл?
Подробнее здесь: https://stackoverflow.com/questions/786 ... le-on-disk