1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\OrangTua\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\OrangTua\Facades\OrangTuaFacade; |
9
|
|
|
|
10
|
|
|
/* Models */ |
11
|
|
|
use Bantenprov\OrangTua\Models\Bantenprov\OrangTua\OrangTua; |
12
|
|
|
use App\User; |
13
|
|
|
|
14
|
|
|
/* Etc */ |
15
|
|
|
use Validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The OrangTuaController class. |
19
|
|
|
* |
20
|
|
|
* @package Bantenprov\OrangTua |
21
|
|
|
* @author bantenprov <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class OrangTuaController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Create a new controller instance. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
protected $orang_tua; |
32
|
|
|
protected $user; |
33
|
|
|
|
34
|
|
|
public function __construct(OrangTua $orang_tua, User $user) |
35
|
|
|
{ |
36
|
|
|
$this->orang_tua = $orang_tua; |
37
|
|
|
$this->user = $user; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Display a listing of the resource. |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\Response |
44
|
|
|
*/ |
45
|
|
|
public function index(Request $request) |
46
|
|
|
{ |
47
|
|
|
if ($request->has('sort')) { |
48
|
|
|
list($sortCol, $sortDir) = explode('|', $request->sort); |
49
|
|
|
|
50
|
|
|
$query = $this->orang_tua->orderBy($sortCol, $sortDir); |
51
|
|
|
} else { |
52
|
|
|
$query = $this->orang_tua->orderBy('id', 'asc'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($request->exists('filter')) { |
56
|
|
|
$query->where(function($q) use($request) { |
57
|
|
|
$value = "%{$request->filter}%"; |
58
|
|
|
$q->where('nomor_un', 'like', $value) |
59
|
|
|
->orWhere('no_kk', 'like', $value); |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
64
|
|
|
$response = $query->paginate($perPage); |
65
|
|
|
|
66
|
|
|
foreach($response as $user){ |
67
|
|
|
array_set($response->data, 'user', $user->user->name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return response()->json($response) |
71
|
|
|
->header('Access-Control-Allow-Origin', '*') |
72
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Show the form for creating a new resource. |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Http\Response |
79
|
|
|
*/ |
80
|
|
|
public function create() |
81
|
|
|
{ |
82
|
|
|
$users = $this->user->all(); |
83
|
|
|
|
84
|
|
|
foreach($users as $user){ |
85
|
|
|
array_set($user, 'label', $user->name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response['user'] = $users; |
|
|
|
|
89
|
|
|
$response['loaded'] = true; |
90
|
|
|
|
91
|
|
|
return response()->json($response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Display the specified resource. |
96
|
|
|
* |
97
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
98
|
|
|
* @return \Illuminate\Http\Response |
99
|
|
|
*/ |
100
|
|
|
public function store(Request $request) |
101
|
|
|
{ |
102
|
|
|
$orang_tua = $this->orang_tua; |
103
|
|
|
|
104
|
|
|
$validator = Validator::make($request->all(), [ |
105
|
|
|
'user_id' => 'required', |
106
|
|
|
'nomor_un' => 'required|max:255', |
107
|
|
|
'no_kk' => 'required|max:255', |
108
|
|
|
'no_telp' => 'required|max:255', |
109
|
|
|
'nama_ayah' => 'required|max:255', |
110
|
|
|
'nama_ibu' => 'required|max:255', |
111
|
|
|
'pendidikan_ayah' => 'required|max:255', |
112
|
|
|
'kerja_ayah' => 'required|max:255', |
113
|
|
|
'pendidikan_ibu' => 'required|max:255', |
114
|
|
|
'kerja_ibu' => 'required|max:255', |
115
|
|
|
'alamat_ortu' => 'required|max:255', |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
if($validator->fails()){ |
119
|
|
|
$check = $orang_tua->where('label',$request->label)->whereNull('deleted_at')->count(); |
120
|
|
|
|
121
|
|
|
if ($check > 0) { |
122
|
|
|
$response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
|
|
|
|
123
|
|
View Code Duplication |
} else { |
|
|
|
|
124
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
125
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
126
|
|
|
$orang_tua->no_kk = $request->input('no_kk'); |
127
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
128
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
129
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
130
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
131
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
132
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
133
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
134
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
135
|
|
|
$orang_tua->save(); |
136
|
|
|
|
137
|
|
|
$response['message'] = 'success'; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
} else { |
|
|
|
|
141
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
142
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
143
|
|
|
$orang_tua->no_kk = $request->input('no_kk'); |
144
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
145
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
146
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
147
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
148
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
149
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
150
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
151
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
152
|
|
|
$orang_tua->save(); |
153
|
|
|
$response['message'] = 'success'; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$response['loaded'] = true; |
157
|
|
|
|
158
|
|
|
return response()->json($response); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Store a newly created resource in storage. |
163
|
|
|
* |
164
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
165
|
|
|
* @return \Illuminate\Http\Response |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
170
|
|
|
|
171
|
|
|
$response['user'] = $orang_tua->user; |
|
|
|
|
172
|
|
|
$response['orang_tua'] = $orang_tua; |
173
|
|
|
$response['loaded'] = true; |
174
|
|
|
|
175
|
|
|
return response()->json($response); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Show the form for editing the specified resource. |
180
|
|
|
* |
181
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
182
|
|
|
* @return \Illuminate\Http\Response |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
187
|
|
|
|
188
|
|
|
$response['orang_tua'] = $orang_tua; |
|
|
|
|
189
|
|
|
$response['loaded'] = true; |
190
|
|
|
|
191
|
|
|
return response()->json($response); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Update the specified resource in storage. |
196
|
|
|
* |
197
|
|
|
* @param \Illuminate\Http\Request $request |
198
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
199
|
|
|
* @return \Illuminate\Http\Response |
200
|
|
|
*/ |
201
|
|
|
public function update(Request $request, $id) |
202
|
|
|
{ |
203
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
204
|
|
|
|
205
|
|
|
$validator = Validator::make($request->all(), [ |
206
|
|
|
'nomor_un' => 'required|max:255', |
207
|
|
|
'no_kk' => 'required|max:255', |
208
|
|
|
'no_telp' => 'required|max:255', |
209
|
|
|
'nama_ayah' => 'required|max:255', |
210
|
|
|
'nama_ibu' => 'required|max:255', |
211
|
|
|
'pendidikan_ayah' => 'required|max:255', |
212
|
|
|
'kerja_ayah' => 'required|max:255', |
213
|
|
|
'pendidikan_ibu' => 'required|max:255', |
214
|
|
|
'kerja_ibu' => 'required|max:255', |
215
|
|
|
'alamat_ortu' => 'required|max:255', |
216
|
|
|
]); |
217
|
|
|
|
218
|
|
|
if($validator->fails()){ |
219
|
|
|
$response['error'] = true; |
|
|
|
|
220
|
|
|
$response['message'] = $validator->errors()->first(); |
221
|
|
|
} else { |
222
|
|
|
$orang_tua->user_id = $request->user_id; |
223
|
|
|
$orang_tua->nomor_un = $request->nomor_un; |
224
|
|
|
$orang_tua->no_kk = $request->no_kk; |
225
|
|
|
$orang_tua->no_telp = $request->no_telp; |
226
|
|
|
$orang_tua->nama_ayah = $request->nama_ayah; |
227
|
|
|
$orang_tua->nama_ibu = $request->nama_ibu; |
228
|
|
|
$orang_tua->pendidikan_ayah = $request->pendidikan_ayah; |
229
|
|
|
$orang_tua->kerja_ayah = $request->kerja_ayah; |
230
|
|
|
$orang_tua->pendidikan_ibu = $request->pendidikan_ibu; |
231
|
|
|
$orang_tua->kerja_ibu = $request->kerja_ibu; |
232
|
|
|
$orang_tua->alamat_ortu = $request->alamat_ortu; |
233
|
|
|
$orang_tua->save(); |
234
|
|
|
|
235
|
|
|
$response['error'] = false; |
|
|
|
|
236
|
|
|
$response['message'] = 'Success'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$response['loaded'] = true; |
240
|
|
|
|
241
|
|
|
return response()->json($response); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Remove the specified resource from storage. |
246
|
|
|
* |
247
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
248
|
|
|
* @return \Illuminate\Http\Response |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
253
|
|
|
|
254
|
|
|
if ($orang_tua->delete()) { |
255
|
|
|
$response['status'] = true; |
|
|
|
|
256
|
|
|
} else { |
257
|
|
|
$response['status'] = false; |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return json_encode($response); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.