1

I wanna ask something about export excel using laravel and some plugin maatwebsite/excel, everything is ok when I through the normal tutorial and like a normal people is working without error, but my problem is when I try make a custom query to my ExampleExport.php (not real) and the result of my excel file is not downloading, I see my result on my console, is doesnt showing anything error, but when I click in network tab and click again to my url the error message show like this :

The GET method is not supported for this route. Supported methods: POST.

anyway my error is normal when you try accessing post method and that error will be show, but is it have another way to solve my problem, so that I can download my excel file? I hope there's any way to make my problem solve, well I wanna put my code in below so that everyone can know what the problem that I have, thank you before, have a nice day

JQUERY :

 // export excel button click
 $("#export_excel").on('click', function () {
      const yearInput = $("#year_input").val();
      // get api data only yearInput
      $.ajax({
           url: `report-payment/filter-data/year_result=${yearInput}`,
           method: "GET",
           data: {
                yearInput: yearInput
           },
           success: function (success) {
                *...* <= I skip this code
                 $.ajax({
                     url: `report-payment/export-excel`,
                     type: "POST",
                     dataType: "json",
                     data: {
                          nameOccupant: infoNameOccupant,
                          roomOccupant: infoRoomOccupant,
                          locationRoomOccupant: infoLocationRoomOccupant,
                          infoPayment: infoPayment
                     },
                     success: function (success) {
                          if (success) {
                               console.log('success');
                          }
                     }
                });
           }
       });
   });

CONTROLLER :

public function paymentExportExcel(Request $filter)
{
    $get_name_occupant = $filter->nameOccupant;
    $get_room_occupant = $filter->roomOccupant;
    $get_location_room_occupant = $filter->locationRoomOccupant;
    $get_info_payment = $filter->infoPayment;

    return Excel::download(new PaymentExport($get_name_occupant, $get_room_occupant, $get_location_room_occupant, $get_info_payment), 'report-payment.xlsx');
}

FILE EXPORT :

public function collection()
{
    for ($payment = 0; $payment < count($this->info_payment); $payment++) { 
        $raw_info_payment[] = [
            'info_payment' => $this->info_payment[$payment]
        ];
    }

    for ($payment = 0; $payment < count($raw_info_payment); $payment++) { 
        $get_arr_info_payment[] = \array_map(function ($value) {
            return $value == "-1" || $value == "-" ? "Belum Lunas" : $value;
        }, $raw_info_payment[$payment]['info_payment']);
    }

    // make a new object to insert a new data
    for ($data = 0; $data < count($this->name_occupant); $data++) { 
        $data_info_payment[] = [
            "name_occupant" => $this->name_occupant[$data],
            "room_occupant" => $this->room_occupant[$data],
            "location_room_occupant" => $this->location_room_occupant[$data],
            "info_payment" => $this->info_payment[$data]
        ];
    }

    return collect($data_info_payment);
}
ProLuck
  • 331
  • 4
  • 18

1 Answers1

0

When you want a normal download, you cant do it with ajax call.

You can make a hidden form with data setup using jquery but you need to submit the form (like when you send the user to another URL when submitted) and that will trigger the download without the user leaving the page.

Why suggest a form submit ? because your route is a POST request, and you cant do it with a simple redirect.

If you want to handle the download with JS, and setup a blob manualy; follow this answer https://stackoverflow.com/a/9970672/4369919

N69S
  • 16,110
  • 3
  • 22
  • 36