File: /var/www/admin.fixgini.com/app/Livewire/Dashboard/Users.php
<?php
namespace App\Livewire\Dashboard;
use App\Models\User;
use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
class Users extends Component
{
public $role = 'buyer';
public $status = 'active';
public $roleLabel;
public $label;
public $paginateUrl;
public $getUsers = [];
public $totalUsers = 0;
public $page = 1;
public $getPag;
protected $rules = [
'role' => 'sometimes|exists:users,role',
'status' => 'sometimes|exists:users,status',
];
public function mount()
{
$this->updateUsers($this->role, $this->status);
}
public function loadMore()
{
$this->page++;
$this->updateUsers($this->role, $this->status, $this->page);
}
public function updateUsers($role = null, $status = null, $pageUrl = null)
{
$this->role = $role ?? $this->role;
$this->status = $status ?? $this->status;
try {
$query = User::query();
if (!empty($this->role)) {
$query->where('role', $this->role);
}
if (!empty($this->status)) {
$query->where('status', $this->status);
}
// $users = $query->with('wallet')->orderBy('id', 'desc')->paginate(1);
$users = User::where('status', $this->status)->where('role', $this->role)->latest()->paginate(2);
// $users = $query->with('wallet')->orderBy('id', 'desc')->paginate(2, ['*'], 'page', $this->page);
// dd($users);
// Extract user data and pagination info
$this->getUsers = $users->items(); // Just the data array
$this->totalUsers = $users->total();
$this->roleLabel = $this->getRoleLabel($this->role);
$this->paginateUrl = $users->nextPageUrl();
} catch (\Throwable $th) {
Log::info($th->getMessage());
$this->addError('error', 'Unable to fetch users');
}
}
private function getRoleLabel($role)
{
$labels = [
'buyer' => 'Customer',
'seller' => 'Provider',
'admin' => 'Administrator',
];
return $labels[$role] ?? 'User';
}
public function render()
{
$users = User::where('status', $this->status)
->where('role', $this->role)
->latest()
->paginate(20);
$this->getUsers = $users->items(); // Just the data array
$this->totalUsers = $users->total();
$this->roleLabel = $this->getRoleLabel($this->role);
$this->paginateUrl = $users->nextPageUrl();
return view('livewire.dashboard.users', [
'users' => $users,
'getUsers' => $this->getUsers,
'totalUsers' => $this->totalUsers,
'roleLabel' => $this->roleLabel,
'paginateUrl' => $this->paginateUrl,
]);
}
}