Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

younesdiouri's avatar

Please Help !

Hello guys , I'm using Laravel 5.4 and Maatwebsite Excel export for my app. I got a very serious issue ! if someone could help me :) I'm using a form , passing data => controller who's handling this data / creating a query / passing to query to the view into a . All usual.... But, I want to export that data (the same query I got from my controller) so I put a EXPORT button . I created a ServiceProvider for the exportHandling, creating my sheet, getting the $data, formatting the rows etc.

1)This EXPORT button cannot redirect to my action because I don't have the same $request anymore.

  1. I can't put the Export Code into the controller, because if I go to the route it will download automatically the .xls file witout displaying the view (and its redondant).
  2. I'm kinda lost !

Thanks for the probable guardian angel that will understand this mess. ;)

0 likes
4 replies
jlrdw's avatar

A work around just have one controller method for viewing, another for exporting.

ohffs's avatar

In the olden days I did this by using a bit of javascript which looped over the table (it was a huge report) then sending it to the back-end to re-export as Excel.

Is your controller taking lots of filters or something? Can you make the export button have a copy of the query/filters (ie, you end up with a button that points to /export?filter1=red&filter2=large"?

Or if the data isn't too huge, duplicate it as a javascript/JSON variable and send that to your export controller/route when the user clicks?

Or if your data is being displayed in a table maybe look at using the jquery datatables excel/csv exporting.

1 like
jimmck's avatar
  1. create spreadsheet using lib and write to a serverside file. Then open that file set the mime type for applicatioin/excel and return that bye stream to browser. You will have to manage the file that was created. I.E. either delete or store it for later.
1 like
younesdiouri's avatar

@jldrw its very big queries, I can't redo the same queries twice, one for exporting / one for viewing, and I have alot of filters - group accounts, so my controllers are too big. Its a solution I know, but not optimized at all :/

@ohffs the data result is not that huge but making the query is very long :) ! And the filter interface is different from the result one (too much filters) so the get method can't be applied here :/ . But you are saying that my controller is sending a $data table towards my blade and I can send it back to the same controller with an ajax that will activate the export service provider/ or an export controller ? --> I think thats a good idea ! (thanks !! /D) Finally, the datatable export plugin can't work because i'm doing rounds on values and formatting them on the . I can only use Maatwebsite ExcelExport (very good) for laravel because I can build my spreadsheet the way I want to ^^

@jimmck I will try that one too ! My ExcelPhp can handle that for sure. I will have to test how much resources it takes, and compare it to @ohffs idea. thanks :D

Thanks you all guys !

1 like

Please or to participate in this conversation.