SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.28k stars 1.92k forks source link

[Bug]: export generator made duplicate rows #4184

Open normatov07 opened 2 months ago

normatov07 commented 2 months ago

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

What version of Laravel Excel are you using?

3.1

What version of Laravel are you using?

10.10

What version of PHP are you using?

8.2

Describe your issue

When I use generator with lazy model query. it gives some duplicate rows instead real rows. the count of rows is equal with real ones but real rows is lost and some rows become duplicate.


class ListExportGenerator implements FromGenerator, WithHeadings, ShouldAutoSize
{
    use Exportable;

    protected $query;

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

    public function generator(): Generator
    {
        foreach ($this->query->lazy(200) as $item) {

       yield [
                $createdAt,
                $updatedAt,
                $this->getStatus(intval($item["status"] ?? "")),
            ];
           }
   }
 public function headings(): array
    {
        return [
            'Дата создания',
            'Дата изменения',
            'Статус',
        ];
    }

    protected function getStatus($status): string
    {
        return match ($status) {
            default => "Не активный",
        };
    }
}
```__

### How can the issue be reproduced?

I use yield with generator.

### What should be the expected behaviour?

It must contain real rows, not duplicate.
patrickbrouwers commented 2 months ago

Why not just use FromQuery + WithMapping, it does the same as your example?

normatov07 commented 2 months ago

Why not just use FromQuery + WithMapping, it does the same as your example?

FromQuery, can I use this as lazy loading ? . I will test it later. For now I resolve it by FromCollection inter and prepareRows method.

chatisk commented 1 month ago

Why not just use FromQuery + WithMapping, it does the same as your example?

Because FromQuery, although it chunks the jobs according to the query's count (AppendQueryToSheet), Close Sheet is not optimised / adjusted for that use, leading to memory allocation exhaustion.