0

I developed a Laravel application that connects to a external MySQL database for reports. I'm using Maatwebsite/Laravel-Excel 3.1 to be able to export to excel.

In this app, I have a method in my model that uses Input::get() for 3 variables ($from, $to, $paymentType). This method is called whenever the user chooses date range and payment type as filter for their reports. Everything works as it should since data is displayed in my view.

Now this same method is called when the user chooses to export the file to excel. Again, all that works except the files is blank.

The curious thing is that if I replace all Input::get() for static values such as '2018-11-14' for the dates and 'n' for payment type, then the file exports with data.

I've been struggling with this for a couple of days so I hope someone can help me.

Thanks,

Ernesto

  • 1
    What version of Laravel are you using? `Input::get()` is deprecated and you should use `$request->input()` instead. – hktang Nov 14 '18 at 14:01
  • Sorry, I'm using Laravel 5.6. When I use `$request->input()` I get Call to undefined method Illuminate\Database\Eloquent\Builder::input(). – orpheus779 Nov 14 '18 at 14:44
  • Which format are you trying to create e.g. csv, xls, or xlsx? Also are you creating exporting excel file from view, from collection or from array. Can you please add your part of the code (from controller) where you are exporting it and/or any other relevant source snippet. This will help to identify the cause. – Muhammad Sheraz Nov 28 '18 at 12:04

2 Answers2

0

If the parameters are obtained from the request you should use $request->input() instead of Input::get().

If used in a function where $request is not available you could use request('variable') to get the variable value from the request.

  • When I use $request->input() I get Call to undefined method Illuminate\Database\Eloquent\Builder::input(). – orpheus779 Nov 14 '18 at 14:47
  • Where are you using the code: controller, model, repository....? Could you provide the code in your questions? – Adrian Hernandez-Lopez Nov 14 '18 at 14:49
  • It's being used in model. I could try to post my code but have to edit it to hide table names, etc. – orpheus779 Nov 14 '18 at 15:00
  • I get the same result, blank excel. Could it be an issue with the Export model instead? I have this in mi export model:
    `class ReportsExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnFormatting { /** * @return \Illuminate\Support\Collection */ public function query() { return Report::docs()->orderBy('desc_doc'); }`
    – orpheus779 Nov 14 '18 at 15:10
  • So the issue is that the file is empty, not the request parameters. Correct? – Adrian Hernandez-Lopez Nov 14 '18 at 15:12
  • Correct. Only the file is empty. If I put fixed values, then the file exported contains data. – orpheus779 Nov 14 '18 at 15:14
  • In that case I'd suggest to review the package documentation about how to include parameters https://laravel-excel.maatwebsite.nl/3.1/exports/from-query.html. If the parameters are not always present you can use Conditional queries to handle that case more info at https://laravel.com/docs/5.7/queries#conditional-clauses – Adrian Hernandez-Lopez Nov 14 '18 at 15:18
  • I did a dd() to this line in my export model: `dd(Report::docs()->orderBy('desc_doc'));` and compared it to a dd() with fixed values. Turns out it's showing values as null when using `request('variable')`. Just need to figure out why. – orpheus779 Nov 14 '18 at 15:26
0

First, towards the top of the controller file, add:

use Illuminate\Http\Request;

Then, you can access the fields by:

$request->input('from');
$request->input('to');

Assumption: from and to are correct field names.

You can also use request()->input('from') etc. directly (note the absence of $).

Please see this for more info: https://laravel.com/docs/5.6/requests

hktang
  • 1,745
  • 21
  • 35
  • "You can also use request()->input('from') etc. directly (note the absence of $). " This works but in the same way Input::get(). Meaning it shows all the data in my view but the exported excel is blank. Forgot to mention that I did a dd($from, $to, $paymentType) to make sure the correct values are passed and sure enough they are. – orpheus779 Nov 14 '18 at 14:57
  • @orpheus779 I see. Could you show us the relevant controller and view codes? – hktang Nov 15 '18 at 00:12