Skip to content

Commit

Permalink
fix roles filter
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Apr 25, 2024
1 parent cb6c8f5 commit a25f8f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions resources/views/roles/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<x-slot:header>
{{trans('tomato-roles::global.roles.index')}}
</x-slot:header>
<x-slot:icon>
bx bxs-lock
</x-slot:icon>
<x-slot:buttons>
<x-tomato-admin-button :href="route('admin.roles.create')" type="link">
{{trans('tomato-admin::global.crud.create-new')}} {{trans('tomato-roles::global.roles.single')}}
Expand Down
3 changes: 3 additions & 0 deletions resources/views/users/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<x-slot:header>
{{ trans('tomato-roles::global.users.index') }}
</x-slot:header>
<x-slot:icon>
bx bxs-user
</x-slot:icon>
<x-slot:buttons>
<x-tomato-admin-button :modal="true" :href="route('admin.users.create')" type="link">
{{trans('tomato-admin::global.crud.create-new')}} {{ trans('tomato-roles::global.users.index') }}
Expand Down
8 changes: 8 additions & 0 deletions src/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ class UserController extends Controller
*/
public function index(Request $request): View
{
$query = User::query();
if(isset($request->get('filter')['role_id']) && !empty($request->get('filter')['role_id'])){
$query->whereHas('roles', function($q) use ($request){
$q->where('id', (int)$request->get('filter')['role_id']);
});
}

return Tomato::index(
request: $request,
model: User::class,
view: 'tomato-roles::users.index',
table: \TomatoPHP\TomatoRoles\Tables\UserTable::class,
query: $query
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Tables/UserTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class UserTable extends AbstractTable
*
* @return void
*/
public function __construct()
public function __construct(public mixed $query)
{
//
if(!$query){
$this->query = \App\Models\User::query();
}
}

/**
Expand All @@ -37,7 +39,7 @@ public function authorize(Request $request)
*/
public function for()
{
return \App\Models\User::query();
return $this->query;
}

/**
Expand All @@ -52,11 +54,12 @@ public function configure(SpladeTable $table)

$table
->selectFilter(
key: 'roles.id',
key: 'role_id',
options: $roles->pluck('name', 'id')->toArray(),
label: trans('tomato-roles::global.users.roles'),
noFilterOption: true,
noFilterOptionLabel: trans('tomato-roles::global.users.filters.roles')
noFilterOptionLabel: trans('tomato-roles::global.users.filters.roles'),
applyQuery: false
)
->withGlobalSearch(label: trans('tomato-admin::global.search'),columns: ['id','name','email',])
->bulkAction(
Expand Down

0 comments on commit a25f8f9

Please sign in to comment.