Passed
Push — develop ( 5f0aa7...736d84 )
by Septianata
04:40
created

UserController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Resources\DataTables\UserResource;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Str;
9
use Yajra\DataTables\Facades\DataTables;
10
11
class UserController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return \Illuminate\Contracts\Support\Renderable
17
     */
18
    public function index()
19
    {
20
        return view('user.index');
21
    }
22
23
    /**
24
     * Return datatable server side response.
25
     *
26
     * @return \Illuminate\Http\JsonResponse
27
     */
28
    public function datatable()
29
    {
30
        return DataTables::eloquent(User::query()->with('branch:id,name'))
31
            ->setTransformer(fn ($model) => UserResource::make($model)->resolve())
32
            ->orderColumn('branch_name', function ($query, $direction) {
33
                $query->with(['branch' => function ($query) use ($direction) {
34
                    $query->orderBy('name', $direction);
35
                }]);
36
            })
37
            ->filterColumn('branch_name', function ($query, $keyword) {
38
                $query->with(['branch' => function ($query) use ($keyword) {
39
                    $query->where('name', 'like', $keyword);
40
                }]);
41
            })
42
            ->filterColumn('is_active', function ($query, $keyword) {
43
                $active = Str::lower(trans('Active'));
0 ignored issues
show
Bug introduced by
It seems like trans('Active') can also be of type array and array; however, parameter $value of Illuminate\Support\Str::lower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
                $active = Str::lower(/** @scrutinizer ignore-type */ trans('Active'));
Loading history...
44
                $notActive = Str::lower(trans('Not Active'));
45
46
                if (in_array($keyword, [$active, $notActive])) {
47
                    $query->where('is_active', $keyword === $active);
48
                }
49
            })
50
            ->toJson();
51
    }
52
53
    /**
54
     * Show the form for creating a new resource.
55
     *
56
     * @return \Illuminate\Contracts\Support\Renderable
57
     */
58
    public function create()
59
    {
60
        return view('user.create');
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69
    public function store(Request $request)
70
    {
71
        User::create($request->all());
72
73
        return redirect()->route('user.index');
74
    }
75
76
    /**
77
     * Show the form for editing the specified resource.
78
     *
79
     * @param  \App\Models\User  $user
80
     * @return \Illuminate\Contracts\Support\Renderable
81
     */
82
    public function edit(User $user)
83
    {
84
        return view('user.edit', compact('user'));
85
    }
86
87
    /**
88
     * Update the specified resource in storage.
89
     *
90
     * @param  \Illuminate\Http\Request  $request
91
     * @param  \App\Models\User  $user
92
     * @return \Illuminate\Http\RedirectResponse
93
     */
94
    public function update(Request $request, User $user)
95
    {
96
        $user->update($request->all());
97
98
        return redirect()->route('user.index');
99
    }
100
101
    /**
102
     * Remove the specified resource from storage.
103
     *
104
     * @param  \App\Models\User  $user
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107
    public function destroy(User $user)
108
    {
109
        $user->delete();
110
111
        return redirect()->route('user.index');
112
    }
113
}
114