3

I have the following laravel excel export:

namespace App\Admin\Exports\Items;

use App\Flare\Models\Item;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;

class ItemsExport implements FromQuery, ShouldQueue {

    use Exportable;

    public function query() {
        return Item::all();
    }
}

Which is called in this controller action:

public function export() {

    (new ItemsExport)->queue('items.xlsx')->chain([
        new ExportItemsEmail(auth()->user()),
    ]);

    return redirect()->back()->with('success', 'File will be emailed to you once done.');
}

There over 5k records for this table

Horizon pushes the job properly but one of them fails:

Maatwebsite\Excel\Jobs\AppendQueryToSheet

ArgumentCountError: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed > in /home/person/Documents/flare/vendor/maatwebsite/excel/src/Jobs/AppendQueryToSheet.php on line 96 and at least 1 expected in /home/person/Documents/flare/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:407

Is this not how you would queue up a large export that should chunk the results of the query?

I have to append a job so I can't just store it. Am I doing something wrong?

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Maybe don't use `Item::all()`, but rather `Item::query()`? I'm not sure of the inner workings of what's going on here, but if this is trying to chain functions on the `Builder` class, eventually executing `->get()` to finalize the query, `Item::all()` won't work, as that returns a `Collection`. – Tim Lewis Jun 16 '21 at 17:57
  • Have you tried to use `->get()` on your `Item::all()` ? – Ezra Lazuardy Jun 16 '21 at 18:39
  • 1
    @TimLewis `::query()` was correct, how ever now the job does not chunk into multiple jobs and thus it times out. – TheWebs Jun 16 '21 at 18:44
  • @EzraLazuardy That's how this error was called... `all()` returns a `Collection`, and `->get()` on a `Collection` requires at least 1 argument. `::all()` is just objectively wrong here. @TheWebs Unfortunately that part is beyond my scope of knowledge; I've never used the `ShouldQueue` trait of `laravel-excel`, sorry. Hopefully someone else can help – Tim Lewis Jun 16 '21 at 18:47
  • 1
    @TimLewis Well the main issue has been solved so I can answer and close this question. thanks :D – TheWebs Jun 16 '21 at 18:52

1 Answers1

0

The proper way to avoid the error is to use Item::query() as all will return a collection.

TheWebs
  • 12,470
  • 30
  • 107
  • 211