laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

Error on pagination

Open Alexxosipov opened this issue 1 year ago • 1 comments

Hey there! There's an error when I'm trying to click on the 2nd (or any another) page.

For example: we're building a table which will show last user's sessions Steps to reproduce:

  1. Create a new table, add some property to a table on which the builder depends (for example User $user)
  2. Add some optional conditions in the builder() method which depends on a property from the mount(User $user) method

Expected result: we have a table with list of user's sessions with ability to see the second page. Current result: Typed property App\Http\Livewire\UserSessionsTable::$user must not be accessed before initialization on click on the 2nd page.

Code of the component:

class UserSessionsTable extends DataTableComponent
{
    protected $model = User::class;

    private User $user;

    public function configure(): void
    {
        $this->setPrimaryKey('id');
    }

    public function mount(User $user)
    {
        $this->user = $user;
    }

    public function builder(): Builder
    {
        return Session::where('user_id', $this->user->id);
    }

    public function columns(): array
    {
        return [
            Column::make("Id", "id")
                ->sortable(),
            Column::make("Session name", "name")
                ->sortable(),
        ];
    }
}

Alexxosipov avatar Sep 08 '22 11:09 Alexxosipov

I may have to stand corrected, but livewire won't maintain private/protected properties between Livewire updates, per the official documentation.

If this is for your end users (rather than admins) then use the Auth::id() rather than instantiating the full user, and you won't have to worry about it.

If this is for admin users, you may want to consider using a public property for the User ID, and setting appropriate security in place if you're concerned about disclosure.

lrljoe avatar Sep 10 '22 05:09 lrljoe

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.

stale[bot] avatar Oct 11 '22 00:10 stale[bot]