Error when exporting using queue, auth user and filter with relation #97

Closed
opened 2023-08-03 07:44:08 +00:00 by ArnaudBan · 7 comments
ArnaudBan commented 2023-08-03 07:44:08 +00:00 (Migrated from github.com)

I have create the smallest test project to reproduce the bug :
https://github.com/ArnaudBan/filament-excel-export-reproduce-bug

There is a bug when :

  1. you set the export to use fromTable() and ->queue()
ExcelExport::make('table')
   ->fromTable()
    ->queue()
  1. In the filament ressource you have a query that depends on the auht()->user
public static function getEloquentQuery(): Builder
{
    return parent::getEloquentQuery()
       ->where('city_id', auth()->user()->city_id);
}
  1. You have a filter with a relation field
->filters([
    Tables\Filters\SelectFilter::make('place')
        ->relationship('city', 'name')
])

Then when you export you have an error because the export try to get the query of the filament resource, but when it is done asynchronously there is no auth user

ErrorException: Attempt to read property "city_id" on null in /Users/arnaudbanvillet/Sites/export-bug/app/Filament/Resources/UserResource.php:27

I have create the smallest test project to reproduce the bug : https://github.com/ArnaudBan/filament-excel-export-reproduce-bug There is a bug when : 1. you set the export to use `fromTable()` and `->queue()` ``` ExcelExport::make('table') ->fromTable() ->queue() ``` 2. In the filament ressource you have a query that depends on the `auht()->user` ``` public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->where('city_id', auth()->user()->city_id); } ``` 3. You have a filter with a relation field ``` ->filters([ Tables\Filters\SelectFilter::make('place') ->relationship('city', 'name') ]) ``` Then when you export you have an error because the export try to get the query of the filament resource, but when it is done asynchronously there is no auth user > ErrorException: Attempt to read property "city_id" on null in /Users/arnaudbanvillet/Sites/export-bug/app/Filament/Resources/UserResource.php:27
pxlrbt commented 2023-08-03 08:34:27 +00:00 (Migrated from github.com)

Thanks for creating an issue and providing a reproduction repo. I am not really sure how to tackle this though, since currently we just serialize the closures and of course the user gets lost on the queue. Need to think of some workarounds.

I probably won't have time to look into this in the next weeks as I still have some client work and still haven't upgraded my plugins for Filament v3 which is a priority for me.

Thanks for creating an issue and providing a reproduction repo. I am not really sure how to tackle this though, since currently we just serialize the closures and of course the user gets lost on the queue. Need to think of some workarounds. I probably won't have time to look into this in the next weeks as I still have some client work and still haven't upgraded my plugins for Filament v3 which is a priority for me.
MichaelMdp commented 2023-08-28 14:19:33 +00:00 (Migrated from github.com)

I have similar issues when using Auth::user() in a closure of a filter method (e.g. visible()), even without using ->fromTable().

Another related issue is global scopes using Auth::user().
As a workaround I use the ->modifyQueryUsing(...) method on the ExcelExport::make() method, to force my global scope.

Did you find any solution ?

I have similar issues when using Auth::user() in a closure of a filter method (e.g. visible()), even without using ->fromTable(). Another related issue is global scopes using Auth::user(). As a workaround I use the ->modifyQueryUsing(...) method on the ExcelExport::make() method, to force my global scope. Did you find any solution ?
pxlrbt commented 2023-08-28 14:38:01 +00:00 (Migrated from github.com)

No, sorry. Didn't have the time to look into this and it wasn't my priority so far, since there were other bugs. When I think about it: A possible solution would be to store the current user with the job and authenticate as that user before running the queue.

But I think I need to overwrite the Job classes from the excel package. Sounds like it would take quite some time.

No, sorry. Didn't have the time to look into this and it wasn't my priority so far, since there were other bugs. When I think about it: A possible solution would be to store the current user with the job and authenticate as that user before running the queue. But I think I need to overwrite the Job classes from the excel package. Sounds like it would take quite some time.
nitinatvaluelabs commented 2024-02-16 07:47:30 +00:00 (Migrated from github.com)

Anybody know any work-around with this one? Seems like I have to remove queue() method till this bug gets resolved.

Anybody know any work-around with this one? Seems like I have to remove `queue()` method till this bug gets resolved.
sadekd commented 2024-02-16 07:49:56 +00:00 (Migrated from github.com)

@nitinatvaluelabs try official Export Action added in FilamentPhp V3

https://filamentphp.com/docs/3.x/actions/prebuilt-actions/export

@nitinatvaluelabs try official Export Action added in FilamentPhp V3 https://filamentphp.com/docs/3.x/actions/prebuilt-actions/export
pxlrbt commented 2024-02-16 10:24:48 +00:00 (Migrated from github.com)

@sadekd Does the official one support columns based on the logged in user? I think this is more of a new feature than a bug.

@sadekd Does the official one support columns based on the logged in user? I think this is more of a new feature than a bug.
nitinatvaluelabs commented 2024-02-16 18:14:00 +00:00 (Migrated from github.com)

@sadekd Unfortunately, I am stuck with version 2 for the next two months. :(

@sadekd Unfortunately, I am stuck with version 2 for the next two months. :(
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
pxlrbt/filament-excel#97
No description provided.