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

HasRecords::resolveTableRecord doesn't work with orWhere in a filter #14179

Open
dcaswel opened this issue Sep 8, 2024 · 3 comments
Open

HasRecords::resolveTableRecord doesn't work with orWhere in a filter #14179

dcaswel opened this issue Sep 8, 2024 · 3 comments
Labels

Comments

@dcaswel
Copy link

dcaswel commented Sep 8, 2024

Package

filament/filament

Package Version

v3.2.110

Laravel Version

v11.21.0

Livewire Version

v3.5.6

PHP Version

PHP 8.3.10

Problem description

I have a CopyResource with a table that has multiple actions on each row and the ListCopies page has a few tabs that filter what is being shown in the table. If I activate one of the tabs that uses orWhere then click on an action button in one of the rows at the bottom, it will perform the action on the wrong copy.

Expected behavior

With the setup described in the problem description, clicking on the action button should perform the action on the record that had the button.

Steps to reproduce

  1. Create a model with a status column (Copy)
  2. Create a Resource (CopyResource)
  3. In the table add an action button similar to:
Action::make('post)->action(fn($record) => $record->post())->button();
  1. In the List page (ListCopies) implement the getTabs() method and return a tab that uses orWhere similar to:
Tab::make('Purchased')
    ->modifyQueryUsing(fn($query) => $query->where('status', 'purchased')->orWhere('status', 'available'))
  1. Create a bunch of records that have one of those 2 statuses
  2. Go to the resource page and click on the tab from step 4
  3. Click on the action button at the bottom of the table
  4. This will run the action on a different record

The attached repo has a README with steps for reproducing this in that project as well as some information I found on a partial cause for the issue.

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

https://github.com/dcaswel/filament-issue

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
@dcaswel dcaswel added bug Something isn't working medium priority unconfirmed labels Sep 8, 2024
@hamidroohani
Copy link
Contributor

hamidroohani commented Sep 9, 2024

I realized that when you use orWhere in the tabs, a CancelException is triggered, which causes an unmountTableAction. However, I couldn't find where this exception is being triggered.

https://github.com/filamentphp/filament/blob/3.x/packages/tables/src/Concerns/HasActions.php#L219-L223

@dcaswel
Copy link
Author

dcaswel commented Sep 10, 2024

As far as I can tell, it's not throwing any exceptions. It just passes the wrong record into the action. So the wrong record gets updated.

@JackWH
Copy link
Contributor

JackWH commented Sep 17, 2024

@dcaswel I've been tripped up by this before... Just thinking aloud as I haven't tested, but does the problem go away if you change the query like this?

Tab::make('Purchased')
    ->modifyQueryUsing(fn($query) => $query->where(
        fn($query) => $query->where('status', 'purchased')->orWhere('status', 'available')
    ))

The Laravel docs warn orWhere clauses should always be placed in logical groups. Without doing so, the orWhere you've added might be cancelling out an internal where clause added by Filament.

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

3 participants