laravel-livewire-tables
laravel-livewire-tables copied to clipboard
Error on pagination
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:
- Create a new table, add some property to a table on which the builder depends (for example
User $user
) - Add some optional conditions in the
builder()
method which depends on a property from themount(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(),
];
}
}
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.
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.
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.