I want to download a excel file through Checkbox Unique ID in laravel, I am using Maatwebsite\Excel Here

856 views Asked by At

My Controller File

public function enquiryExport($id, Request $request)
{
    $id[] = $request->explode(",",$id);
    return Excel::download(new EnquiryExport($id), 'enquiry.xlsx');
}

and My Export File

 protected $id;
function __construct($id) {
        $this->id = $id;
 }

public function collection()
{
    return Enquiry::whereIn('id',explode(",",$this->id))->get();

   /* return Enquiry::all(); */
}

Route is like

 Route::get('enquiryExport', 'enquiryController@enquiryExport');

Still I am getting this error

"message": "Too few arguments to function App\\Http\\Controllers\\enquiryController::enquiryExport(), 1 passed and exactly 2 expected",

I am checkbox id through AJAX here.

2

There are 2 answers

0
Ngô Minh On

The problems is your Route method.

Get method: the query string (name/value pairs) is sent in the URL of a GET request

Post method: the data sent to the server with POST is stored in the request body of the HTTP request

If you use Get method: try this (I have just read it, not tried)

Route::get('enquiryExport/{id}', 'enquiryController@enquiryExport')->name('enquiryExport');

<a href="{{ route('enquiryExport', serialize($id)) }}">Submit</a>

If you use Post method: try this (I am used to use this)

Route::post('enquiryExport', 'enquiryController@enquiryExport');

public function enquiryExport(Request $request)
{
    return Excel::download(new EnquiryExport($request->input('id')), 'enquiry.xlsx');
}

You can read more here: https://www.w3schools.com/tags/ref_httpmethods.asp

0
Tahani Alnefaie On

Try this In controller:

   public function enquiryExport(Request $request, $id)
   {
     return Excel::download(new EnquiryExport($request->id), ''.date('Y-m-d'). '.xlsx', \Maatwebsite\Excel\Excel::XLSX);
   }

In Export File:

       protected $id;

        function __construct($id) {
            $this->id = $id;
        }
       public function collection()
        {

          return Enquiry::where('id', $this->id)->get();
       }


       public function map($enquiry): array
{
    
    return [
        // $enquiry->WRITE YOUR RECORDS,
        // ...

    ];
}

public function headings(): array
{
    return [
            //NAME HEADINGS(TITLE) OF YOUR RECORDS IN SIDE SINGLE QUOTATION,
            // ...
    ];
}

In Route:

    Route::get('enquiryExport/{id}', 'enquiryController@enquiryExport');