Completed
Push — manage-users ( afd8af...2c714e )
by Fèvre
02:36
created

UserController::search()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 8.439
cc 5
eloc 28
nc 5
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\User;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Xetaravel\Http\Controllers\Admin\Controller;
8
use Xetaravel\Models\User;
9
10
class UserController extends Controller
11
{
12
    /**
13
     * Show the search page.
14
     *
15
     * @return \Illuminate\View\View
16
     */
17
    public function index(): View
18
    {
19
        $latestUsers = User::with(['roles'])
20
            ->limit(5)
21
            ->latest()
22
            ->get();
23
24
        $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Users', route('admin.user.user.index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
26
        return view('Admin::User.user.index', compact('latestUsers', 'breadcrumbs'));
27
    }
28
    /**
29
     * Search users related to the type.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     *
33
     * @return \Illuminate\View\View
34
     */
35
    public function search(Request $request): View
36
    {
37
        $query = User::with(['roles'])->select();
38
        $search = str_replace('%', '\\%', trim($request->input('search')));
39
        $type = trim($request->input('type'));
40
41
        switch ($type) {
42
            case 'username':
43
                $query->where('username', 'like', '%' . $search . '%');
44
                break;
45
46
            case 'email':
47
                $query->where('email', 'like', '%' . $search . '%');
48
                break;
49
50
            case 'register_ip':
51
                $query->where('register_ip', 'like', '%' . $search . '%');
52
                break;
53
54
            case 'last_login_ip':
55
                $query->where('last_login_ip', 'like', '%' . $search . '%');
56
                break;
57
58
            default:
59
                $query->where('username', 'like', '%' . $search . '%');
60
                $type = 'username';
61
                break;
62
        }
63
        $users = $query
64
            ->paginate(10)
65
            ->appends($request->except('page'));
66
67
        $breadcrumbs = $this->breadcrumbs
68
            ->addCrumb('Manage Users', route('admin.user.user.index'))
69
            ->addCrumb('Search an user', route('admin.user.user.search'));
70
71
        return view('Admin::User.user.search', compact('users', 'breadcrumbs', 'type', 'search'));
72
    }
73
74
    public function showUpdateForm(Request $request, string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $user = User::find($id)->with(['Account'])->first();
77
78
        return view('Admin::User.user.update', compact('user'));
79
    }
80
}
81