Filter doesn't work when having `having` in the query

In UserResource i want to filter users that have more then X number of device tokens.

I'm doing something like:

Tables\Filters\Filter::make('is_blocked')
  ->label('Blocked users')
  ->query(fn ($query) => $query
    ->withCount('deviceTokens')
    ->having('device_tokens_count', '>=', 4)
  )
  ->toggle(),

But when I activate the filter the SQL query that's being sent to database doesn't seems to be changed.


Normally in Laravel thinker when I write something like:
 User::query()->withCount('deviceTokens')->having('device_tokens_count', '>=', 4)->get()


It works fine.

Any ideas ?
Solution
I solved it using:

Tables\Filters\Filter::make('is_blocked')
  ->label('Blocked users')
  ->query(fn (Builder $query) => $query->has('deviceTokens', '>=', DeviceToken::maxAllowedTokens) )
  ->toggle()
Was this page helpful?