Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

controller.plain.stub 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace {{ namespace }};
  3. use {{ rootNamespace }}Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use App\Helpers\Helper;
  7. use Validator;
  8. use Hashids;
  9. use Yajra\Datatables\Datatables;
  10. use Auth;
  11. use Illuminate\Support\Facades\Route;
  12. //load modelmu
  13. class {{ class }} extends Controller
  14. {
  15. /**
  16. * Title untuk judul di web
  17. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  18. */
  19. private $title = '{{ namespace }}'; /**jangan lupa diganti*/
  20. private $route = 'nama_path_awal.nama_path_akhir.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  21. private $header = 'Headermu';
  22. private $sub_header = 'Sub Headermu';
  23. /**khusus namespace ambil pathnya aja contoh : App\Http\Controllers\Admin\Simdondar\DonorDarah -> Admin\Simdondar\DonorDarah*/
  24. private $namespace_controller = '{{ namespace }}';
  25. private $url_prefix = 'urlnya routenya';
  26. public function __construct()
  27. {
  28. DB::enableQueryLog();
  29. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  30. //$this->middleware('permission:Nama_Role_yang_dibuat-list|Nama_Role_yang_dibuat-create|Nama_Role_yang_dibuat-update|Nama_Role_yang_dibuat-delete', ['only' => ['index', 'create', 'update']]);
  31. //$this->middleware('permission:Nama_Role_yang_dibuat-create', ['only' => ['create', 'create_action']]);
  32. //$this->middleware('permission:Nama_Role_yang_dibuat-update', ['only' => ['update', 'update_action']]);
  33. //$this->middleware('permission:Nama_Role_yang_dibuat-delete', ['only' => ['delete']]);
  34. }
  35. /**
  36. * Ini contoh crud yang sudah jalan
  37. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  38. * Untuk CRUD Biar cepat
  39. * Silahkan ganti
  40. * EloquentMu:: => dengan model anda
  41. * $id_modelmu => ganti dengan id di model anda
  42. * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
  43. */
  44. public function index()
  45. {
  46. Helper::swal();
  47. $data = [
  48. //bawaan
  49. 'title' => $this->title,
  50. 'route' => $this->route,
  51. 'header' => $this->header,
  52. 'sub_header' => $this->sub_header,
  53. ];
  54. // dd($data);
  55. return view($this->route . 'index', $data);
  56. }
  57. /**
  58. *digunakan untuk datatable yajra
  59. *querynya bisa ganti" sesuai hati
  60. */
  61. public function getData()
  62. {
  63. $query = EloquentMu::All();
  64. $data_table = Datatables::of($query)
  65. ->addColumn('aksi', function ($query) {
  66. /**cek role */
  67. $aksi = '';
  68. if (Auth::user()->can('Nama_Role_yang_dibuat-update')) {
  69. $aksi .= "<a href='" . route($this->route . 'update', ['id_di_routenya' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  70. }
  71. if (Auth::user()->can('Nama_Role_yang_dibuat-delete')) {
  72. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['id_di_routenya' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  73. }
  74. return $aksi;
  75. })
  76. ->rawColumns(['aksi'])
  77. ->escapeColumns([]) //digunakan untuk render html
  78. ->toJson();
  79. return $data_table;
  80. }
  81. /**
  82. * create digunakan untuk menampilkan tampilan buat
  83. */
  84. public function create()
  85. {
  86. $data = [
  87. //bawaan
  88. 'title' => $this->title,
  89. 'route' => $this->route,
  90. 'header' => $this->header,
  91. 'sub_header' => $this->sub_header,
  92. ];
  93. Helper::swal();
  94. return view($this->route . 'create', $data);
  95. }
  96. /**
  97. * create_action digunakan untuk aksi post
  98. * lengkap dengan validatornya
  99. */
  100. public function create_action(Request $request)
  101. {
  102. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  103. $post = $request->all();
  104. /**digunakan untuk set rule validator */
  105. $rules = [];
  106. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  107. $alert = [];
  108. $validator = Validator::make($post, $rules, $alert);
  109. if ($validator->passes()) {
  110. /**menggunakan transaction */
  111. DB::beginTransaction();
  112. $query = EloquentMu::create($post);
  113. if ($query) {
  114. DB::commit();
  115. $message = 'Berhasil';
  116. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  117. } else {
  118. DB::rollback();
  119. $message = 'Gagal';
  120. return redirect()->back()->with('error', Helper::parsing_alert($message));
  121. }
  122. }
  123. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  124. $message = Helper::parsing_alert($validator->errors()->all());
  125. return redirect()->back()->with('error', $message)->withInput();
  126. }
  127. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  128. public function update($id_modelmu)
  129. {
  130. Helper::swal();
  131. $id_modelmu = Hashids::decode($id_modelmu);
  132. if (!empty($id_modelmu)) {
  133. $cek_data = EloquentMu::where('id', $id_modelmu[0])->first();
  134. if ($cek_data) {
  135. $data = [
  136. //bawaan
  137. 'title' => $this->title,
  138. 'route' => $this->route,
  139. 'header' => $this->header,
  140. 'sub_header' => $this->sub_header,
  141. 'data' => $cek_data
  142. ];
  143. return view($this->route . 'update', $data);
  144. }
  145. $message = 'Id tidak ditemukan atau sudah dihapus';
  146. return redirect()->back()->with('error', $message);
  147. }
  148. $message = 'Id tdak ditemukan';
  149. return redirect()->back()->with('error', $message);
  150. }
  151. /**update_action POST
  152. * Logikanya :
  153. * 1. Cek Hashids::decode dari data_id apalah benar" di hash ?
  154. * -iya
  155. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  156. * -iya
  157. * 3. proses validate
  158. * -iya
  159. * 4. Proses update
  160. * - iya benar
  161. * - tidak salah
  162. * - tidak return error
  163. * - tidak return Id tdak ditemukan
  164. * -tidak skip reutrn id null
  165. */
  166. public function update_action(Request $request)
  167. {
  168. $post = $request->all();
  169. /**digunakan untuk set rule validator */
  170. $rules = [];
  171. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  172. $alert = [];
  173. $validator = Validator::make($post, $rules, $alert);
  174. $id_modelmu = Hashids::decode($request['id']);
  175. if (!empty($id_modelmu)) {
  176. /**cek apakah data_idnya ada ? */
  177. $cek_data = EloquentMu::where('id', $id_modelmu[0])->first();
  178. if ($cek_data) {
  179. /**cek apakah id data benar" ada di DB ? */
  180. if ($validator->passes()) {
  181. /**validatornya */
  182. DB::beginTransaction();
  183. $query = $cek_data->update($post);
  184. if ($query) {
  185. DB::commit();
  186. $message = 'Berhasil';
  187. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  188. } else {
  189. DB::rollback();
  190. $message = 'Gagal';
  191. return redirect()->back()->with('error', Helper::parsing_alert($message));
  192. }
  193. }
  194. $message = Helper::parsing_alert($validator->errors()->all());
  195. return redirect()->back()->with('error', $message);
  196. } else {
  197. $message = 'Id tdak ditemukan';
  198. return redirect()->back()->with('error', $message);
  199. }
  200. } else {
  201. $message = 'Id tidak boleh kosong';
  202. return redirect()->back()->with('error', $message);
  203. }
  204. }
  205. /**digunakan untuk delete
  206. * Logikanya :
  207. * 1. Cek hash id
  208. * -ya
  209. * 2. Cek id dari hash
  210. * - ya
  211. * 3. Proses delete
  212. * -Ya
  213. * -Tidak
  214. * - tidak return Id tidak ditemukan atau sudah dihapus
  215. * -tidak reutrn Id tdak ditemukan
  216. */
  217. public function delete_action($id_modelmu)
  218. {
  219. $id_modelmu = Hashids::decode($id_modelmu);
  220. /**cek idnya apakah ada ? */
  221. if (!empty($id_modelmu)) {
  222. $cek_data = EloquentMu::where('id', $id_modelmu[0])->first();
  223. if ($cek_data) {
  224. DB::beginTransaction();
  225. $delete = $cek_data->delete();
  226. if ($delete) {
  227. DB::commit();
  228. $message = 'Succesfully';
  229. return redirect()->back()->with('success', $message);
  230. } else {
  231. DB::rollback();
  232. $message = 'Gagal';
  233. return redirect()->back()->with('error', $message);
  234. }
  235. }
  236. $message = 'Id tidak ditemukan atau sudah dihapus';
  237. return redirect()->back()->with('error', $message);
  238. }
  239. $message = 'Id tdak ditemukan';
  240. return redirect()->back()->with('error', $message);
  241. }
  242. /**declare routenya disni*/
  243. public function route(){
  244. $route = Route::namespace($this->namespace_controller)
  245. ->prefix($this->url_prefix)
  246. ->name($this->route)
  247. ->group(function () {
  248. Route::get('/', ['as' => 'index', 'uses' => '{{ class }}@index']);
  249. Route::get('/get-data/{udd_id?}', ['as' => 'get_data', 'uses' => '{{ class }}@getData']);
  250. Route::get('/datatables', ['as' => 'datatables', 'uses' => '{{ class }}@datatables']);
  251. Route::get('/create', ['as' => 'create', 'uses' => '{{ class }}@create']);
  252. Route::get('/update/{id_di_routenya}', ['as' => 'update', 'uses' => '{{ class }}@update']);
  253. Route::get('/delete-action/{id_di_routenya}', ['as' => 'delete_action', 'uses' => '{{ class }}@delete_action']);
  254. Route::post('/create-action', ['as' => 'create_action', 'uses' => '{{ class }}@create_action']);
  255. Route::post('/update-action', ['as' => 'update_action', 'uses' => '{{ class }}@update_action']);
  256. });
  257. return $route;
  258. }
  259. /**end declare route */
  260. }