Hide all records in the table

Hi, I have a question: how can I hide all records in the table as long as a filter option is not selected? For example, records should only appear when I select an academic period; otherwise, nothing should appear in the table.
No description
No description
Solution:
Thank you very much, I have modified it a little and it worked. ```php ->modifyQueryUsing(fn ($query, $livewire) => $query ->when(...
Jump to solution
10 Replies
Dennis Koch
Dennis Koch6mo ago
You could do something like this to filter all before a filter was selected:
->modifyQueryUsing(fn ($query, $livewire) => $query->when(
blank($livewire->tableFilters),
fn ($query) => $query->whereRaw('1 <> 1')
))
->modifyQueryUsing(fn ($query, $livewire) => $query->when(
blank($livewire->tableFilters),
fn ($query) => $query->whereRaw('1 <> 1')
))
aacarrion4
aacarrion4OP6mo ago
Sorry, where should this code go?
Dennis Koch
Dennis Koch6mo ago
On the table definition of your resource Not 100% sure whether it's correct but should point you in the right direction
Solution
aacarrion4
aacarrion46mo ago
Thank you very much, I have modified it a little and it worked.
->modifyQueryUsing(fn ($query, $livewire) => $query
->when(
blank($livewire->tableFilters),
fn ($query) => $query->whereRaw('1 <> 1')
)
->when(
$livewire->tableFilters['academic_period_id'] ?? null,
fn ($query, $academic_period_id) => $query->where('academic_period_id', $academic_period_id)
)
->when(
$livewire->tableFilters['course_id'] ?? null,
fn ($query, $course_id) => $query->where('course_id', $course_id)
))
->modifyQueryUsing(fn ($query, $livewire) => $query
->when(
blank($livewire->tableFilters),
fn ($query) => $query->whereRaw('1 <> 1')
)
->when(
$livewire->tableFilters['academic_period_id'] ?? null,
fn ($query, $academic_period_id) => $query->where('academic_period_id', $academic_period_id)
)
->when(
$livewire->tableFilters['course_id'] ?? null,
fn ($query, $course_id) => $query->where('course_id', $course_id)
))
Dennis Koch
Dennis Koch6mo ago
You shouldn’t need to apply filters manually though
aacarrion4
aacarrion4OP6mo ago
I understand, but for this table I needed it that way, since I don't want all the records to be mixed up.
Dennis Koch
Dennis Koch6mo ago
Not sure what you mean be „mixed up“ but the filter behavior should be the same as your query 🤔
aacarrion4
aacarrion4OP6mo ago
That is to say, I don't want all the records of the table to appear, only the filtered ones, otherwise it will be too confusing.
Dennis Koch
Dennis Koch6mo ago
Which is what a filter should do. Right? 😅 Filter plus the addition we did in the beginning.
aacarrion4
aacarrion4OP6mo ago
Yes, in the end the result would be like this, I hide the columns in the table but I show them in the filter
No description
Want results from more Discord servers?
Add your server