【问题标题】:Laravel Excel is not working when queuingLaravel Excel 在排队时不起作用
【发布时间】:2021-09-01 13:43:30
【问题描述】:

我有以下 laravel excel 导出:

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();
    }
}

在这个控制器动作中被调用:

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.');
}

此表有超过 5k 条记录

Horizo​​n 正确推送作业,但其中一个失败:

Maatwebsite\Excel\Jobs\AppendQueryToSheet

ArgumentCountError: Too little arguments to function Illuminate\Support\Collection::get(), 0 pass > in /home/person/Documents/flare/vendor/maatwebsite/excel/src/Jobs/AppendQueryToSheet.php 在第 96 行并且在 /home/person/Documents/flare/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:407 中预计至少有 1 个

这难道不是您将应该分块查询结果的大型导出排队的方式吗?

我必须附加一个作业,所以我不能只存储它。 我做错了吗?

【问题讨论】:

  • 也许不要使用Item::all(),而是使用Item::query()?我不确定这里发生了什么的内部运作,但如果这是试图链接 Builder 类上的函数,最终执行 ->get() 以完成查询,Item::all() 将不起作用,因为那样返回一个Collection
  • 您是否尝试在您的Item::all() 上使用->get()
  • @TimLewis ::query() 是正确的,但现在该作业没有分成多个作业,因此它超时了。
  • @EzraLzuardy 这就是这个错误的调用方式...all() 返回一个Collection,而->get()Collection 上至少需要1 个参数。 ::all() 在这里客观上是错误的。 @TheWebs 不幸的是,这部分超出了我的知识范围;我从未使用过laravel-excelShouldQueue 特征,抱歉。希望其他人可以提供帮助????
  • @TimLewis 好吧,主要问题已经解决,所以我可以回答并关闭这个问题。谢谢:D

标签: php excel laravel laravel-excel


【解决方案1】:

避免错误的正确方法是使用Item::query(),因为所有都将返回一个集合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多