Completed
Push — master ( fb5d13...91116a )
by
unknown
25s queued 11s
created

DoctorController::searchByRadio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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