FilamentF
Filament10mo ago
Batman

Modify query when loading edit/view form

What is the best way to modify the query when loading an edit or view page?

Using soft deletes. So when I select a user in the table, I need to modify the query so that a soft deleted user can be loaded for editing/viewing. Otherwise, nothing is returned. This results in a 404 - Not Found when loading edit / view resource.
Solution
@awcodes Believe I got it figured out.
public static function getEloquentQuery(): Builder  
{  
    if (Auth::user()->hasRole(Roles::SUPER_ADMIN)) {  
        // Check if the resource page is view or edit and if true then disable global scope:  
        if (  
            Route::currentRouteName() === Pages\EditUser::getRouteName('admin')  
            || Route::currentRouteName() === Pages\ViewUser::getRouteName('admin')  
        ) {  
            return parent::getEloquentQuery()  
                ->withoutGlobalScopes([  
                    SoftDeletingScope::class,  
                ]);  
        }  
    }  
  
    return parent::getEloquentQuery();  
}


Found in this discussion: https://github.com/filamentphp/filament/discussions/10042
GitHub
Sometimes we have a different behavior when you are in an edit or creating form using a resource, so for that, we need to identify what is the page that is active at the moment. For example, if we ...
Was this page helpful?