rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.76k stars 330 forks source link

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

Closed lrljoe closed 9 months ago

lrljoe commented 11 months ago

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

lrljoe commented 11 months ago

Just testing the fix

lrljoe commented 11 months ago

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

mtt786 commented 10 months ago

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();
}
RenoLooijmans commented 10 months ago

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.

lrljoe commented 10 months ago

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()

stale[bot] commented 9 months ago

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.

siebsie23 commented 8 months ago

@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 https://github.com/rappasoft/laravel-livewire-tables/issues/1491#issuecomment-1831332636 fixes the problem for me. Was the fix you made already implemented in the code, or is it broken again by later updates?