0

Context: I have an export that I want to mail to its intended recipients once it is stored.

I do this inside a Job class.

public function handle(): void
{
    (new ReportExport($this->report, $this->date))->queue($this->report->path)
        ->chain([
            new NotifyReportRecipients($this->report),
        ]);
}

Inside the export, I'm using the FromGenerator concern.

public function generator(): Generator
{
    $query = DB::query()->select(...)->from(...)->where(...);

    return $query->cursor()->getIterator();
}

This already works.


Now, I want to send a different notification but only in the case the export is empty, so I'm trying to cause an exception so I can handle it in the Job class.

use Path\To\MyException;

public function generator(): Generator
{
    $query = DB::query()->select(...)->from(...)->where(...);

    throw_if($query->count() === 0, MyException::class);

    return $query->cursor()->getIterator();
}

But it does not work since the Laravel-excel developers didn't implement a catch method for their QueuedExport class.

public function handle(): void
{
    (new ReportExport($this->report, $this->date))->queue($this->report->path)
        ->chain([
            new NotifyReportRecipients($this->report),
        ])
        ->catch(function (MyException $e) {
            new NotifyReportRecipientsAboutEmptyReport($this->report),
        });
        //  Error  Call to undefined method Maatwebsite\Excel\Jobs\QueueExport::catch()
}

I've tried using a try...catch block

public function handle(): void
{
    try {
        (new ReportExport($this->report, $this->date))->queue($this->report->path)
            ->chain([
                new NotifyReportRecipients($this->report),
            ]);
    } catch (MyException $e) {
        NotifyReportRecipientsAboutEmptyReport::dispatch($this->report);
    }
}

But it does not work.

I've tried following the documentation for Laravel excel and just add a failed method to my Export class but this doesn't work either.

use Path\To\MyException;

public function failed(Throwable $e)
{
    if ($e instanceof MyException) {
        NotifyReportRecipientsAboutEmptyReport::dispatch($this->report);
    }
}

public function generator(): Generator
{
    $query = DB::query()->select(...)->from(...)->where(...);

    throw_if($query->count() === 0, MyException::class);

    return $query->cursor()->getIterator();
}

What's worse, is that the chain just dispatches the Notification regardless.

Does anyone know the correct way to deal with this?

2
  • failed method in the export class was added since version 3.1.25 of laravel excel. what version do you have installed? Commented Mar 19, 2024 at 1:43
  • According to the lock file and composer show, I have the 3.1.51 version installed. Commented Mar 19, 2024 at 12:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.