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/User/Index.php
<?php

namespace App\Livewire\User;

use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
    public $name, $lastname, $email, $phone, $role, $status, $userId;

    use WithPagination;

    public $search = '';
    public $sortField = 'name';
    public $sortDirection = 'asc';
    public $perPage = 5;

    public function sortBy($field)
    {
        $this->sortDirection = $this->sortField === $field
            ? ($this->sortDirection === 'asc' ? 'desc' : 'asc')
            : 'asc';
        $this->sortField = $field;
    }

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function confirmDeletion($userId)
    {
        try {
            $user = User::findOrFail($userId);
            $user->delete();
            session()->flash('message', 'User deleted successfully.');
        } catch (\Throwable $e) {
            session()->flash('error', 'An error occurred while deleting the user.');
        }
    }
    public function editUser($id)
    {
        $user = User::findOrFail($id);
        $this->userId = $user->id;
        $this->name = $user->name;
        $this->lastname = $user->lastname;
        $this->email = $user->email;
        $this->phone = $user->phone;
        $this->role = $user->role;
        $this->status = $user->status;
        $this->dispatch('show-modal');
    }

    public function saveUser(): void
    {
        $user = User::find($this->userId);
        $user->update([
            'name' => $this->name,
            'lastname' => $this->lastname,
            'email' => $this->email,
            'phone' => $this->phone,
            'role' => $this->role,
            'status' => $this->status,
        ]);
        session()->flash('message', 'User updated successfully.');
        $this->reset();
        $this->dispatch('hide-modal');
    }

    public function render()
    {
        return view('livewire.user.index', [
            'users' => User::query()
                ->where('role', 'buyer') // Ensure the role is 'seller'
                ->where(function ($query) { // Group the search conditions together
                    $query->where('name', 'like', '%' . $this->search . '%')
                        ->orWhere('lastname', 'like', '%' . $this->search . '%')
                        ->orWhere('email', 'like', '%' . $this->search . '%');
                })
                ->orderBy($this->sortField, $this->sortDirection)
                ->paginate($this->perPage),
        ]);
    }
}