DoctorController::index()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\DoctorSpecialization;
6
use Illuminate\Database\QueryException;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Hash;
9
use App\Doctor;
10
use App\City;
11
use Psy\Exception\ErrorException;
12
13
14
class DoctorController extends Controller
15
{
16
    /**
17
     * Returning view with data resource
18
     *
19
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
20
     */
21
    public function index()
22
    {
23
        $doctors = Doctor::orderBy('created_at', 'desc')->paginate(10);
24
        if(!$doctors) {
0 ignored issues
show
introduced by
$doctors is of type Illuminate\Pagination\LengthAwarePaginator, thus it always evaluated to true.
Loading history...
25
            abort(503);
26
        }
27
28
        try {
29
            return view('pages.doctor', compact('doctors', $doctors));
30
        } catch (ErrorException $exception) {
31
            abort(503, $exception);
32
        }
33
    }
34
35
    /**
36
     * Show the form for creating a new resource.
37
     *
38
     * @return \Illuminate\Http\Response
39
     */
40
    public function create()
41
    {
42
        $specialization = DoctorSpecialization::pluck('name', 'id');
43
        return view('pages.ext.add-doctor')->with('specialization', $specialization);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.a...tion', $specialization) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param Request $request
50
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
51
     * @throws \Illuminate\Validation\ValidationException
52
     */
53
    public function store(Request $request)
54
    {
55
        $this->validate($request,[
56
            'specialty' => 'required',
57
            'name' => 'required|min:3|max:50',
58
            'license' => 'required|min:3|max:191',
59
            'email' => 'required',
60
            'password' => 'required_with:password_confirmation|same:password_confirmation|min:6',
61
            'password_confirmation' => 'min:6'
62
        ]);
63
64
65
        $doctor = new Doctor;
66
        $doctor->name = $request->input('name');
67
        $doctor->specialization_id = $request->input('specialty');
68
        $doctor->license = $request->input('license');
69
        $doctor->email = $request->input('email');
70
        $doctor->password = Hash::make($request->password);
71
72
        if($doctor->save()) {
73
            return redirect (route('doctor.index'))->with('success', 'Berhasil Menambahkan Dokter !');
74
        }
75
76
        return redirect (route('doctor.index'))->with('failed', 'Gagal menambah dokter !');
77
    }
78
79
    /**
80
     * Display the specified resource.
81
     *
82
     * @param  int  $id
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function show($id)
86
    {
87
        $doctor = Doctor::where('specialization_id',$id)->orderBy('name','asc')->paginate(5);
88
        $location = City::orderBy('name','asc')->pluck('name','id');
89
        $data = [
90
            'doctor' => $doctor,
91
            'location' => $location
92
        ];
93
        return view('listDoctor')->with('data',$data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('listDoctor')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
94
    }
95
96
    public function showDoctor($id)
97
    {
98
        $doctor = Doctor::find($id);
99
100
        return view('viewDoctor')->with('doctor',$doctor);
101
    }
102
    /**
103
     * Show the form for editing the specified resource.
104
     *
105
     * @param  int  $id
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function edit($id)
109
    {
110
        $doctor = Doctor::find($id);
111
        $data = [
112
            'role' => session('role'),
113
            'doctor' => $doctor
114
        ];
115
        return view('pages.ext.edit-dokter')->with('data', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.e...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
116
    }
117
118
    /**
119
     * Update the specified resource in storage.
120
     *
121
     * @param Request $request
122
     * @param $id
123
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
124
     * @throws \Illuminate\Validation\ValidationException
125
     */
126
    public function update(Request $request, $id)
127
    {
128
        $this->validate($request,[
129
            'name' => 'required|min:3|max:50',
130
            'password' => 'required_with:password_confirmation|same:password_confirmation|min:6',
131
            'password_confirmation' => 'min:6'
132
        ]);
133
134
        $pass = Hash::make($request->password);
135
        $doctor = Doctor::find($id);
136
        $doctor->name = $request->input('name');
137
        $doctor->password = $pass;
138
        if($doctor->save()) {
139
            return redirect (route('doctor.index'))->with('success', 'Doktor berhasil di update !');
140
        }
141
142
        return redirect (route('doctor.edit', $id))->with('failed', 'Gagal memperbaharui dokter !');
143
    }
144
145
    public function searchDoctor(Request $request)
146
    {
147
        if($request->nama == null AND $request->location != null){
148
            $doctor = Doctor::where('city_id',$request->location)->orderBy('name','asc')->paginate(5);
149
        }elseif ($request->location == null AND $request->nama != null){
150
            $doctor = Doctor::where('name','LIKE','%'.$request->nama.'%')->orderBy('name','asc')->paginate(5);
151
        }else{
152
            $doctor = Doctor::where('name','LIKE','%'.$request->nama.'%')->where('city_id',$request->location)->orderBy('name','asc')->paginate(5);
153
        }
154
        $location = City::orderBy('name','asc')->pluck('name','id');
155
        $data = [
156
            'doctor' => $doctor,
157
            'location' => $location
158
        ];
159
160
        return view('listDoctor')->with('data',$data);
161
    }
162
163
    public function searchByRadio(Request $request)
164
    {
165
        $locationId = City::where('name','LIKE','%'.$request->location.'%')->pluck('id','name');
166
        $doctor = Doctor::where('city_id',$locationId)->orderBy('name','asc')->paginate(5);
167
        $location = City::orderBy('name','asc')->pluck('name','id');
168
        $data = [
169
            'doctor'=>$doctor,
170
            'location' => $location
171
        ];
172
        return view('listDoctor')->with('data',$data);
173
    }
174
175
    /**
176
     * Remove the specified resource from storage.
177
     *
178
     * @param  int  $id
179
     * @return \Illuminate\Http\Response
180
     */
181
    public function destroy($id)
182
    {
183
        $doctor = Doctor::find($id);
184
        $doctor->delete();
185
186
        return redirect (route('doctor.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('doctor.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
187
    }
188
189
}
190
191