Passed
Push — main ( fcba82...0f5868 )
by PRATIK
14:07 queued 38s
created

UserTable::updatedRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\User;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Livewire\Component;
7
use Livewire\WithPagination;
8
use Illuminate\Support\Facades\Cache;
9
use Pratiksh\Adminetic\Models\Admin\Role;
10
11
class UserTable extends Component
12
{
13
    use WithPagination;
14
15
    protected $paginationTheme = 'bootstrap';
16
17
    public $filter = 1;
18
    public $information;
19
    public $role;
20
    public $search;
21
22
    public function updatedSearch()
23
    {
24
        $this->filter = 2;
25
    }
26
27
    public function updatedRole()
28
    {
29
        $this->filter = 3;
30
    }
31
32
    public function render()
33
    {
34
        $users = $this->getUsers();
35
        $roles = Cache::get('roles', Role::latest()->get());
36
        return view('adminetic::livewire.admin.user.user-table', compact('users', 'roles'));
37
    }
38
39
    protected function getUsers()
40
    {
41
        $filter = $this->filter;
42
        $default = User::with('profile', 'roles');
43
        switch ($filter) {
44
            case 1:
45
                $data = $default->latest();
46
                break;
47
            case 2:
48
                $this->resetPage();
49
                $search = $this->search ?? null;
50
                $data = $default->where('name', 'like', '%' . $search . '%')
51
                    ->orWhere('email', 'like', '%' . $search . '%');
52
                $this->information = 'Showing search results for "' . $search . '"';
53
                break;
54
            case 3:
55
                $this->resetPage();
56
                $role_id = $this->role ?? null;
57
                $data = $role_id == '' ? $default : $default->whereHas('roles', function ($query) use ($role_id) {
58
                    $query->where('role_id', $role_id);
59
                });
60
                break;
61
            default:
62
                $data = $default->latest();
63
                break;
64
        }
65
        return $data->paginate(10);
66
    }
67
}
68