Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure inside getStateUsing() called 3 times per row #14852

Open
btxtiger opened this issue Nov 20, 2024 · 9 comments
Open

Closure inside getStateUsing() called 3 times per row #14852

btxtiger opened this issue Nov 20, 2024 · 9 comments
Labels

Comments

@btxtiger
Copy link

btxtiger commented Nov 20, 2024

Package

filament/tables

Package Version

v3.2.124

Laravel Version

v11.33.2

Livewire Version

default

PHP Version

8.3.13

Problem description

When using ->getStateUsing() to fill a column with a custom value, the Closure seems to be called 3 times per row!
image

If this closure contains a custom query, this will lead to triple query execution per row!
Bildschirmfoto 2024-11-20 um 16 16 22

Expected behavior

The Closure should be executed once on page load

Steps to reproduce

Example Column:

Tables\Columns\TextColumn::make('permission_number')
  ->label('Permissions')
  ->badge()
  ->getStateUsing(function (User $user) {
      return $user->permissions()->count();
  }),

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

https://github.com/btxtiger/filament-getstateusing-bug-14852

Relevant log output

No response

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
Copy link

Hey @btxtiger! We're sorry to hear that you've hit this issue. 💛

However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue?

We need a public GitHub repository which contains a Laravel app with the minimal amount of Filament code to reproduce the problem. Please do not link to your actual project, what we need instead is a minimal reproduction in a fresh project without any unnecessary code. This means it doesn't matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Nov 20, 2024
Copy link

Thank you for providing reproduction steps! Reopening the issue now.

@github-actions github-actions bot reopened this Nov 20, 2024
@github-project-automation github-project-automation bot moved this from Done to Todo in Roadmap Nov 20, 2024
@Cybrarist
Copy link

i think you should use formatStateUsing instead of getStateUsing()

@btxtiger
Copy link
Author

btxtiger commented Nov 20, 2024

@Cybrarist this does not work, since the state of the column should be any query or value (probably also a result of an HTTP request, Cache, other service, ...), and not dependant on a model column.

@btxtiger btxtiger changed the title Query inside getStateUsing called 3 times per row Closure inside getStateUsing() called 3 times per row Nov 20, 2024
@Cybrarist
Copy link

@btxtiger oh ok, i get what you mean, then why not doing it like the following

$table->modifyQueryUsing(function (Builder $query) {
      $query->withCount('permissions');
})

and then you use 'permissions_count' in your TextColumn.

@btxtiger
Copy link
Author

btxtiger commented Nov 21, 2024

@Cybrarist yes this would be a workaround for this specific example, but would not solve the actual problem.
Let me show you a different example:

Tables\Columns\TextColumn::make('permission_granted')
  ->label('Permissions')
  ->badge()
  ->getStateUsing(function (User $user) {
      return Http::get('https://dummy-auth-provider.com/api/check-permissions', ['uuid' => $user->uuid])
          ->json('permissions_granted') ? 'Granted' : 'Not Granted';
  });

In this case, the closure is still executed 3 times, and thus make 3 https requests * number of rows, for 1 page load

@ericmp33
Copy link
Contributor

#14852 (comment)

Seems a very bad idea to get the state of a column by doing an HTTP call for each one.

@btxtiger
Copy link
Author

btxtiger commented Nov 22, 2024

Seems a very bad idea to get the state of a column by doing an HTTP call for each one.

Yeah, but still not targeting the issue. The issue is 3 redundant calls of a function where only one should be. Filament 3 became so much slower (less snappy) than v2, and I suspect this is one issue

@bilalelhaj
Copy link

have the same problem here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

4 participants