Passed
Pull Request — master (#52)
by
unknown
04:27
created

DoctorController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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