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(); }
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(); }
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 ...