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

Filament session key improvement #15785

Merged

Conversation

NankoPrinzhorn
Copy link
Contributor

Description

In some scenarios, the default behavior of the table session, which keeps track of enabled and disabled columns, does not work as expected.

Issue

In our use case, we have multiple Livewire components in different directories with the same class name, for example:

  • App\Livewire\Admin\ManualDeployment\Index.php
  • App\Livewire\Admin\Users\Index.php
  • App\Livewire\Admin\ExposureRequests\Index.php

Each of these classes has its own Filament table and uses the InteractsWithForms trait. However, they all share the same session key from the getTableColumnToggleFormStateSessionKey function.

Cause

This happens because class_basename($this::class); returns "Index" for all three classes. As a result, when an ID column is marked as ->toggable(), its visibility state is shared across all tables.

For example:

  • Enabling the ID column in one table will also enable it in the other two tables.
  • Disabling the ID column in one table will hide it in all tables.

This unintended behavior causes inconsistencies when toggling columns across different tables.


Solution

To resolve this, the session key should be based on the fully qualified class name rather than just the basename. However, to prevent overly long session keys—especially in projects with deeply nested directories—we apply hashing.

Previous Implementation

$table = class_basename($this::class); // -> "Index"

Updated Implementation

$table = hash('sha256', $this::class); // -> "619b97a8ad39037aa755dfc64a1fc00782020e23a7cd4013c8209cd29afe534b"

By using a SHA-256 hash of the full class name, we ensure that each table gets a unique session key while keeping it manageable in length.

Functional Changes

  • Code style has been fixed using composer cs.
  • Changes have been tested to ensure no existing functionality breaks.
  • Documentation is up to date.

Let me know if any additional details are needed! 😊

Copy link
Member

@danharrin danharrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many similar SessionKey(): string methods, can you update them please to be consistent?

@danharrin danharrin added the bug Something isn't working label Mar 6, 2025
@danharrin danharrin added this to the v3 milestone Mar 6, 2025
Copy link
Member

@danharrin danharrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, since this is not sensitive data, lets use MD5 instead of SHA256 so the hashes are shorter

@NankoPrinzhorn NankoPrinzhorn requested a review from danharrin March 6, 2025 07:57
@danharrin danharrin merged commit 1dae771 into filamentphp:3.x Mar 6, 2025
14 checks passed
@danharrin
Copy link
Member

Thanks

@NankoPrinzhorn
Copy link
Contributor Author

Thanks for the merge @danharrin, could you create a new tag with the latest changes maybe? Much appreciate

@danharrin
Copy link
Member

In the meantime before the next release, you can define these methods on the Livewire classes in your app to set this up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants