pxlrbt / filament-excel

Excel Export for Filament Admin Resources
MIT License
323 stars 68 forks source link

`modifyQueryUsing` gives error #125

Closed fazliddin closed 9 months ago

fazliddin commented 9 months ago

Following code gives error:

->fromTable()

->modifyQueryUsing(function (
    \Illuminate\Database\Query\Builder $query,
    \App\Models\Keywords $model
)
{
    return $query->where('project_id', $model->project_id);
}),

Error massage:

App\Filament\Resources\ProjectsResource\RelationManagers\KeywordsRelationManager::App\Filament\Resources\ProjectsResource\RelationManagers\{closure}(): Argument #1 ($query) must be of type Illuminate\Database\Query\Builder, Illuminate\Database\Eloquent\Builder given, called in /var/www/vhosts/tulpor-seo/vendor/pxlrbt/filament-excel/src/Exports/ExcelExport.php on line 264

pxlrbt commented 9 months ago

You defined the wrong type. Please read the error carefully:

Argument #1 ($query) must be of type Illuminate\Database\Query\Builder, Illuminate\Database\Eloquent\Builder given

fazliddin commented 9 months ago

But yes \Illuminate\Database\Query\Builder is given

fazliddin commented 9 months ago

in my code i never use Illuminate\Database\Eloquent\Builder

pxlrbt commented 9 months ago

"Given" means "passed as an argument". Filament uses Eloquent\Builder but you declared, that you want a Query\Builder.

fazliddin commented 9 months ago

OK, I changed code as you suggest:

                        ->fromTable()

                        ->modifyQueryUsing(function (
                            \Illuminate\Database\Eloquent\Builder $query,
                            \App\Models\Keywords $model
                        )
                        {
                            return $query->where('project_id', $model->project_id);
                        }),

Now another problem Too few arguments to function App\Filament\Resources\ProjectsResource\RelationManagers\KeywordsRelationManager::App\Filament\Resources\ProjectsResource\RelationManagers\{closure}(), 1 passed in /var/www/vhosts/tulpor-seo/vendor/pxlrbt/filament-excel/src/Exports/ExcelExport.php on line 264 and exactly 2 expected

Now it says 1 arg is passed, but 2 args are expected.

pxlrbt commented 9 months ago

Yes. Because $model is not injected when using modifyQueryUsing(). I think there is an open PR to add $livewire and $tableQuery. Need to look into that PR first, though. Not really sure how to solve your problem, sorry.

fazliddin commented 9 months ago

I can solve this problem by extending export class. But this concrete situation is result of relying on functional programming too heavily. Anonymous function are hard to read and debug. We invented OOP to be able to extent code, but again we are going back functions.