The issue seems to be with the render method in the UsersTable component. The render method is responsible for rendering the component and returning the view. In this case, it is executing the query twice.
To fix this issue, you can modify the render method to execute the query only once. Here's the modified code:
public function render()
{
$this->authorize('view', User::class);
$users = (new User)->newQuery();
// search for ID if field is set
$search = $this->search;
if ($this->search !== '') {
$users->where(function ($query) use ($search) {
$query->where('id', $search)->orWhere('name', 'like', "%" . $search . "%");
});
}
// prepare basic search string first, make sure we only scope for users, not active sellers
$users->doesntHave('sellerDetails')
->withCount([
'orders as orders_count' => function ($query) {
$query->finalized();
},
]);
// check which user section was requested (using GET parameter inside query)
switch ($this->requestSection) {
case 'banned':
$users->whereHas('sanctions', function ($query) {
$query->active();
})->latest();
break;
}
// attach sort filter
$users->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc');
$users = $users->paginate(25);
return view('livewire.users-table', compact('users'));
}
By assigning the paginated results to the $users variable before returning the view, you ensure that the query is executed only once.
This should resolve the issue of the query being executed twice.