SpartnerNL / Laravel-Nova-Excel

🚀 Supercharged Excel exports for Laravel Nova Resources
https://docs.laravel-excel.com/nova/1.0/
MIT License
374 stars 73 forks source link

[BUG] Error in Nova 4 when trying to use actions in lenses #146

Closed Lx45 closed 2 years ago

Lx45 commented 2 years ago

Prerequisites

Versions

Description

Actions to export entries are only functioning in the resource view, not in lenses.

Steps to Reproduce

Expected behavior:

Get an excel file with chosen entires.

Actual behavior:

Get the following error.

` [2022-05-20 12:40:51] local.ERROR: Method Maatwebsite\LaravelNovaExcel\Requests\ExportLensActionRequest::forAllMatchingResources does not exist. {"userId":1,"exception":"[object] (BadMethodCallException(code: 0): Method Maatwebsite\LaravelNovaExcel\Requests\ExportLensActionRequest::forAllMatchingResources does not exist. at /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php:113) [stacktrace]

0 /Users/lx/dev/laravel/vendor/maatwebsite/laravel-nova-excel/src/Requests/ExportLensActionRequest.php(25): Illuminate\Http\Request->__call('forAllMatchingR...', Array)

1 /Users/lx/dev/laravel/vendor/maatwebsite/laravel-nova-excel/src/Actions/ExportToExcel.php(89): Maatwebsite\LaravelNovaExcel\Requests\ExportLensActionRequest->toExportQuery()

2 /Users/lx/dev/laravel/vendor/laravel/nova/src/Http/Controllers/LensActionController.php(38): Maatwebsite\LaravelNovaExcel\Actions\ExportToExcel->handleRequest(Object(Laravel\Nova\Http\Requests\LensActionRequest))

3 /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): Laravel\Nova\Http\Controllers\LensActionController->store(Object(Laravel\Nova\Http\Requests\LensActionRequest), 'insurances', 'collection-agen...')

4 /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('store', Array)

5 /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(261): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Laravel\Nova\Http\Controllers\LensActionController), 'store')

6 /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(204): Illuminate\Routing\Route->runController()

7 /Users/lx/dev/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(725): Illuminate\Routing\Route->run()`

Additional Information

Actions function in the resource file: image

Actions function in the lenses file: image

Code of the Action file: `class ExportInsuranceAsExcel extends DownloadExcel implements ShouldAutoSize, WithStyles { use InteractsWithQueue, Queueable;

public $name = 'Excel Herunterladen';

public function map($insurance): array
{
    return [
        [
            $insurance->product->pluck('name')->first(),
            $insurance->insurance_date,
            $insurance->email,
            $insurance->street,
            $insurance->city,
            $insurance->postal_code,
            $insurance->country,
            $insurance->product->pluck('price')->first().'€',
            ucfirst(__($insurance->insuranceCustomers->pluck('gender')->first())),
            $insurance->insuranceCustomers->pluck('first_name')->first(),
            $insurance->insuranceCustomers->pluck('last_name')->first(),
            $insurance->insuranceCustomers->pluck('birth_date')->first(),
            $insurance->insuranceCustomers->pluck('additional_insurances')->first()
        ],
    ];
}

public function headings(): array
{
    return [
        [
            'Produkt',
            'Datum',
            'Email',
            'Straße',
            'Stadt',
            'PLZ',
            'Land',
            'Kaufpreis',
            'Geschlecht',
            'Vorname',
            'Nachname',
            'Geburtsdatum',
            'Zusatzversicherungen'
        ]
    ];
}

/**
 * Get the fields available on the action.
 *
 * @param \Laravel\Nova\Http\Requests\NovaRequest $request
 * @return array
 */
public function fields(NovaRequest $request)
{
    return [];
}

public function styles(Worksheet $sheet)
{
    return [
        1 => ['font' => ['bold' => true]]
    ];
}

}`

I was able to fix the Bug by overwriting the handleRequest function within the ExportToExcel file. Please tell me what you think and if needed I can fix it and make a PR.

patrickbrouwers commented 2 years ago

I was able to fix the Bug by overwriting the handleRequest function within the ExportToExcel file

Please provide it as a PR, thanks!