Skip to content

ToggleColumn sortable not working with orderByRaw for roles in Relation Manager #16808

@CoringaWc

Description

@CoringaWc

Package

filament/tables

Package Version

v4.0.0-beta1

Laravel Version

v12.19.3

Livewire Version

v3.6.3

PHP Version

8.4.5

Problem description

I’m using the spatie/laravel-permission package to manage user roles and permissions.

In my UsersRelationManager, which displays a table of users belonging to a Tenant (one-to-many), I have a ToggleColumn called is_admin. Its purpose is to assign or remove the "admin" role when toggled.

I want to make this column sortable to list admins first. However, when I use orderByRaw() in the sortable callback, sorting doesn’t work as expected (see attached image).

Image

Here is my table configuration:

public function table(Table $table): Table 
{
    return $table
        ->columns([
            TextColumn::make('name'),
            TextColumn::make('email'),
            ToggleColumn::make('is_admin')
                ->updateStateUsing(function (User $record, bool $state): void {
                    if ($state) {
                        $record->assignRole('admin');
                    } else {
                        $record->removeRole('admin');
                    }
                })
                ->sortable(true, function (Builder $query, string $direction): void {
                    $query->orderByRaw(<<<SQL
                        exists(
                            select 1
                            from model_has_roles
                            join roles on roles.id = model_has_roles.role_id
                            where model_has_roles.model_id = users.id
                            and model_has_roles.model_type = ?
                            and roles.name = 'admin'
                        ) {$direction}
                    SQL, [User::class])->orderBy('name', 'asc');
                }),
        ])
        ->recordUrl(null)
        ->recordAction(null)
        ->recordActions([
            DeleteAction::make(),
        ]);
}

Problem:

Sorting does not prioritize users with the "admin" role as expected. Instead, the order remains unchanged when clicking the column header.

Expected behavior

The expected behavior is that the ToggleColumn sorting works correctly. When sorting in ascending order, all rows with the toggle enabled (checked) should appear first, followed by the unchecked rows.

Please see the attached image for reference.

Image

Steps to reproduce

  1. Create a Tenant resource.
  2. Create a UsersRelationManager for the User model.
  3. Configure the table as shown in the provided code snippet.

Note: When refreshing the page, the sorting initially renders correctly.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/filamentphp/demo

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions