Limiting Filament Table to User-Accessible in resource
Hello! How are you? I have a resource called SchoolResource for my School model. But I need only the schools that the user has permission to view to appear in the resource table. I already have a very simple method that does this, but I don't know how to limit the filament table.
public EloquentCollection|Collection $records; public function mount(): void { $this->records = auth()->user()->schools(); } public static function form(Form $form): Form { return $form ->schema( [ //Abstracted............ ] )->columns(3); }
public EloquentCollection|Collection $records; public function mount(): void { $this->records = auth()->user()->schools(); } public static function form(Form $form): Form { return $form ->schema( [ //Abstracted............ ] )->columns(3); }
Solution
in the resource override the query and add a scope or a where:
public static function getEloquentQuery(): Builder { $query = parent::getEloquentQuery()-->withGlobalScope( SoftDeletingScope::class, ); return $query; }
public static function getEloquentQuery(): Builder { $query = parent::getEloquentQuery()-->withGlobalScope( SoftDeletingScope::class, ); return $query; }