Passed
Push — develop ( b27e40...fcd18c )
by Septianata
43:54
created

UserController::destroyMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\User\StoreRequest;
7
use App\Http\Requests\User\UpdateRequest;
8
use App\Http\Resources\DataTables\UserResource;
9
use App\Models\Branch;
10
use App\Models\User;
11
use Illuminate\Auth\Events\Registered;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Event;
14
use Illuminate\Support\Str;
15
use Yajra\DataTables\Facades\DataTables;
16
17
class UserController extends Controller
18
{
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @return \Illuminate\Contracts\Support\Renderable
23
     */
24
    public function index()
25
    {
26
        return view('admin.user.index');
27
    }
28
29
    /**
30
     * Return datatable server side response.
31
     *
32
     * @return \Illuminate\Http\JsonResponse
33
     */
34
    public function datatable()
35
    {
36
        return DataTables::eloquent(User::query()->with('branch:id,name'))
37
            ->setTransformer(fn ($model) => UserResource::make($model)->resolve())
38
            ->orderColumn('branch_name', function ($query, $direction) {
39
                $query->join('branches', 'users.branch_id', '=', 'branches.id')
40
                    ->select('users.*', 'branches.id as branch_id', 'branches.name as branch_name')
41
                    ->orderBy('branches.name', $direction);
42
            })
43
            ->filterColumn('branch_name', function ($query, $keyword) {
44
                $query->whereHas('branch', function ($query) use ($keyword) {
45
                    $query->where('name', 'like', '%' . $keyword . '%');
46
                });
47
            })
48
            ->filterColumn('is_active', function ($query, $keyword) {
49
                $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

49
                $active = Str::lower(/** @scrutinizer ignore-type */ trans('Active'));
Loading history...
50
                $notActive = Str::lower(trans('Not Active'));
51
52
                if (in_array($keyword, [$active, $notActive])) {
53
                    $query->where('is_active', $keyword === $active);
54
                }
55
            })
56
            ->toJson();
57
    }
58
59
    /**
60
     * Show the form for creating a new resource.
61
     *
62
     * @return \Illuminate\Contracts\Support\Renderable
63
     */
64
    public function create()
65
    {
66
        return view('admin.user.create');
67
    }
68
69
    /**
70
     * Store a newly created resource in storage.
71
     *
72
     * @param  \App\Http\Requests\User\StoreRequest  $request
73
     * @return \Illuminate\Http\RedirectResponse
74
     */
75
    public function store(StoreRequest $request)
76
    {
77
        /** @var \App\Models\User $user */
78
        $user = User::make($request->all())->setBranchRelationValue(Branch::find($request->input('branch_id')));
79
80
        $user->save();
81
82
        $user->syncRoles($request->input('role'));
83
84
        Event::dispatch(new Registered($user));
85
86
        return redirect()->route('admin.user.index')->with([
87
            'alert' => [
88
                'type' => 'alert-success',
89
                'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.user')]),
90
            ],
91
        ]);
92
    }
93
94
    /**
95
     * Show the form for editing the specified resource.
96
     *
97
     * @param  \App\Models\User  $user
98
     * @return \Illuminate\Contracts\Support\Renderable
99
     */
100
    public function edit(User $user)
101
    {
102
        return view('admin.user.edit', compact('user'));
103
    }
104
105
    /**
106
     * Update the specified resource in storage.
107
     *
108
     * @param  \App\Http\Requests\User\UpdateRequest  $request
109
     * @param  \App\Models\User  $user
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function update(UpdateRequest $request, User $user)
113
    {
114
        /** @var \App\Models\User $user */
115
        $user = $user->fill($request->all())->setBranchRelationValue(Branch::find($request->input('branch_id')));
116
117
        $user->save();
118
119
        $user->syncRoles($request->input('role'));
120
121
        return redirect()->route('admin.user.index')->with([
122
            'alert' => [
123
                'type' => 'alert-success',
124
                'message' => trans('The :resource was updated!', ['resource' => trans('admin-lang.user')]),
125
            ],
126
        ]);
127
    }
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param  \App\Models\User  $user
133
     * @return \Illuminate\Http\RedirectResponse
134
     */
135
    public function destroy(User $user)
136
    {
137
        $user->delete();
138
139
        return redirect()->route('admin.user.index')->with([
140
            'alert' => [
141
                'type' => 'alert-success',
142
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
143
            ],
144
        ]);
145
    }
146
147
    /**
148
     * Remove the specified list of resource from storage.
149
     *
150
     * @param  \Illuminate\Http\Request  $request
151
     * @return \Illuminate\Http\RedirectResponse
152
     */
153
    public function destroyMultiple(Request $request)
154
    {
155
        User::destroy($request->input('checkbox', []));
156
157
        return redirect()->route('admin.user.index')->with([
158
            'alert' => [
159
                'type' => 'alert-success',
160
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
161
            ],
162
        ]);
163
    }
164
}
165