Skip to content

Commit

Permalink
feat: change model filter type (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizCristino authored Feb 5, 2024
1 parent c111580 commit da8e1a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/filament-spatie-roles-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

'default_guard_name' => null,

'model_filter_key' => 'return \'%\'.$key;', // Eg: 'return \'%\'.$key.'\%\';'
'model_filter_key' => 'return \'%\'.$value;', // Eg: 'return \'%\'.$key.'\%\';'

'user_name_column' => 'name',

Expand Down
38 changes: 24 additions & 14 deletions src/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource\Pages\ViewPermission;
use Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource\RelationManager\RoleRelationManager;
use Filament\Facades\Filament;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
Expand All @@ -20,7 +19,7 @@
use Filament\Tables;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -121,24 +120,35 @@ public static function table(Table $table): Table
->searchable(),
])
->filters([
Filter::make('models')
SelectFilter::make('models')
->label('Models')
->form(function () {
->multiple()
->options(function () {
$commands = new \Althinect\FilamentSpatieRolesPermissions\Commands\Permission();

/** @var \ReflectionClass[] */
$models = $commands->getAllModels();

return array_map(function (\ReflectionClass $model) {
return Checkbox::make($model->getShortName());
}, $models);
$options = [];

foreach ($models as $model) {
$options[$model->getShortName()] = $model->getShortName();
}

return $options;
})
->query(function (Builder $query, array $data) {
return $query->where(function (Builder $query) use ($data) {
foreach ($data as $key => $value) {
if ($value) {
$query->orWhere('name', 'like', eval(config('filament-spatie-roles-permissions.model_filter_key')));
if (isset($data['values'])) {
$query->where(function (Builder $query) use ($data) {
foreach ($data['values'] as $key => $value) {
if ($value) {
$query->orWhere('name', 'like', eval(config('filament-spatie-roles-permissions.model_filter_key')));
}
}
}
});
});
}

return $query;
}),
])->actions([
Tables\Actions\EditAction::make(),
Expand All @@ -150,7 +160,7 @@ public static function table(Table $table): Table
]),
BulkAction::make('Attach to roles')
->action(function (Collection $records, array $data): void {
Role::whereIn('id',$data['roles'])->each(function (Role $role) use ($records): void {
Role::whereIn('id', $data['roles'])->each(function (Role $role) use ($records): void {
$records->each(fn (Permission $permission) => $role->givePermissionTo($permission));
});
})
Expand Down

0 comments on commit da8e1a8

Please sign in to comment.