rappasoft/laravel-livewire-tables

[Bug]: Using when() in builder() for getAppliedFilterWithValue() can be a lifecycle behind

lrljoe opened this issue · 7 comments

What happened?

Using a getAppliedFilterWithValue can be a lifecycle behind, and thus you don't see the filter applied until the following lifecycle:

public function builder(): Builder
{
    return User::query()
         ->when($this->getAppliedFilterWithValue('active'), fn($query, $active) => $query->where('active', $active === 'yes'));
}

This is likely due to the sequence that everything is being applied.

How to reproduce the bug

No response

Package Version

3.0.0

PHP Version

8.2.x

Laravel Version

10.x

Alpine Version

3.13.2

Theme

Tailwind 3.x

Notes

No response

Error Message

No response

Just testing the fix

This will be in the release after the next one, as I've already shoved a ton into the next release.

I have fixed this by overriding the render method in the child class of the livewire component.

/**
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
 */
public function render(): \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View | \Illuminate\Contracts\Foundation\Application
{
    //Sets up the Builder Instance
    $this->setBuilder($this->builder());
    return parent::render();
}

I have fixed this by overriding the render method in the child class of the livewire component.

/**
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
 */
public function render(): \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View | \Illuminate\Contracts\Foundation\Application
{
    //Sets up the Builder Instance
    $this->setBuilder($this->builder());
    return parent::render();
}

Interesting! I might apply that as well in the meanwhile. Thanks for sharing.

There's some fixes coming in the next version for this. As I've changed the order that different elements apply, to make this available in the initial call to builder()

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@lrljoe I'm still having this issue in v3 when using when(! $this->getSort('created_at') and when(empty($this->getSearch())

The manual fix in this issue #1491 (comment) fixes the problem for me. Was the fix you made already implemented in the code, or is it broken again by later updates?