Skip to content

[Bug]: Search breaks when re-using columns with hideIf attribute #2277

Open
@Asharif88

Description

@Asharif88

What happened?

Hello,

Thanks a lot for making an amazing package.

I am using the builder function to customize the query based on a "type" that is set separately
then in the columns definition I'm adding a column twice with 2 different conditions because I have 2 separate displays that each has a separate behavior, for example one that shows all records and another showing only active ones

When searching the result returns with all the records since it seems searchable for the hidden field still gets applied, and the query is encapsulated with an OR

The issue seems to be in the WithSearch trait as a possible solution would be to ignore hidden columns from search (or another way)

Thanks a lot!

How to reproduce the bug

Configure your builder and columns similar to below

public function builder(): Builder
    {
   if ($this->type == 'page') {
            return Model::query()
                ->where('active', '=', 'true');
        } else {
            return Model::query();
        }
    }

then in the columns definition I'm adding a column twice with 2 different conditions

public function columns(): array
    {
return [
//// Other columns
                Column::make(__('control.title', 2), 'title')
                    ->searchable()
                    ->view('livewire.table.properties')
                    ->sortable()
                    ->hideIf($this->type == 'page'),
                Column::make(__('control.alternate_title'), 'title')
                    ->searchable()
                    ->view('livewire.table.other_properties')
                    ->sortable()
                    ->hideIf($this->type != 'page')
/// Other columns
];
   }

In the component, do a quick search, you will notice that the result will ignore the condition placed in the builder because searchable adds a where and an orWhere condition to the query, callbacks on searchable are also useless because of the same point

Package Version

3.7.1

PHP Version

8.3.x

Laravel Version

12.3.0

Alpine Version

3.13.10

Theme

Bootstrap 5.x

Notes

No response

Error Message

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions