No Description
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.

KelompokDriverController.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Http\Controllers\Admin\Raharja;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use App\Helpers\Helper;
  7. use App\Model\Raharja\KelompokDriver;
  8. use Yajra\Datatables\Datatables;
  9. use Auth;
  10. use Illuminate\Support\Facades\Storage;
  11. use Validator;
  12. use Hashids;
  13. //load modelmu
  14. class KelompokDriverController extends Controller
  15. {
  16. /**
  17. * Title untuk judul di web
  18. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  19. */
  20. private $title = 'Kelompok Driver'; /**jangan lupa diganti*/
  21. private $route = 'admin.kelompok-driver.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  22. private $header = 'Kelompok Driver';
  23. private $sub_header = 'Kelompok Driver';
  24. private $url = 'admin.raharja.kelompok_driver.';
  25. public function __construct()
  26. {
  27. DB::enableQueryLog();
  28. }
  29. public function index()
  30. {
  31. Helper::swal();
  32. $data = [
  33. //bawaan
  34. 'title' => $this->title,
  35. 'route' => $this->route,
  36. 'header' => $this->header,
  37. 'sub_header' => $this->sub_header,
  38. ];
  39. return view($this->url . 'index', $data);
  40. }
  41. public function getData($udd_id = null)
  42. {
  43. $query = KelompokDriver::query();
  44. return Datatables::of($query)
  45. ->addIndexColumn()
  46. ->addColumn('aksi', function ($query) {
  47. /**cek role */
  48. $aksi = '';
  49. $aksi .= "<a href='" . route($this->route . 'update', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  50. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  51. return $aksi;
  52. })
  53. ->rawColumns(['aksi'])
  54. ->toJson();
  55. }
  56. public function create()
  57. {
  58. $data = [
  59. //bawaan
  60. 'title' => $this->title,
  61. 'route' => $this->route,
  62. 'header' => $this->header,
  63. 'sub_header' => $this->sub_header,
  64. ];
  65. Helper::swal();
  66. return view($this->url . 'create', $data);
  67. }
  68. public function create_action(Request $request)
  69. {
  70. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  71. $post = $request->all();
  72. $file = $request->file('logo');
  73. /**digunakan untuk set rule validator */
  74. $rules = [
  75. 'logo' => 'required|file|max:1000',
  76. 'nama' => 'required',
  77. 'website' => 'required',
  78. 'deskripsi' => 'required',
  79. ];
  80. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  81. $alert = [
  82. 'file' => 'File maksimal 1MB'
  83. ];
  84. $validator = Validator::make($post, $rules, $alert);
  85. if ($validator->passes()) {
  86. /**menggunakan transaction */
  87. DB::beginTransaction();
  88. $nama_file = time()."_".$file->getClientOriginalName();
  89. Storage::putFileAs('public/images/kelompok_driver/', $file, $nama_file );
  90. $query = new KelompokDriver();
  91. $query->logo = $nama_file;
  92. $query->nama = $request->nama;
  93. $query->website = $request->website;
  94. $query->deskripsi = $request->deskripsi;
  95. $query->save();
  96. if ($query) {
  97. DB::commit();
  98. $message = 'Berhasil';
  99. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  100. } else {
  101. DB::rollback();
  102. $message = 'Gagal';
  103. return redirect()->back()->with('error', Helper::parsing_alert($message));
  104. }
  105. }
  106. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  107. $message = Helper::parsing_alert($validator->errors()->all());
  108. return redirect()->back()->with('error', $message)->withInput();
  109. }
  110. public function update($data_id)
  111. {
  112. Helper::swal();
  113. $data_id = Hashids::decode($data_id);
  114. if (!empty($data_id)) {
  115. $cek_data = KelompokDriver::where('id', $data_id[0])->first();
  116. if ($cek_data) {
  117. $data = [
  118. //bawaan
  119. 'title' => $this->title,
  120. 'route' => $this->route,
  121. 'header' => $this->header,
  122. 'sub_header' => $this->sub_header,
  123. 'data' => $cek_data
  124. ];
  125. return view($this->url . 'update', $data);
  126. }
  127. $message = 'Id tidak ditemukan atau sudah dihapus';
  128. return redirect()->back()->with('error', $message);
  129. }
  130. $message = 'Id tdak ditemukan';
  131. return redirect()->back()->with('error', $message);
  132. }
  133. public function update_action(Request $request)
  134. {
  135. $post = $request->all();
  136. /**digunakan untuk set rule validator */
  137. $rules = [
  138. 'logo' => 'file|max:1000',
  139. 'nama' => 'required',
  140. 'website' => 'required',
  141. 'deskripsi' => 'required',
  142. ];
  143. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  144. $alert = [];
  145. $validator = Validator::make($post, $rules, $alert);
  146. $data_id = Hashids::decode($request['id']);
  147. if (!empty($data_id)) {
  148. /**cek apakah data_idnya ada ? */
  149. $query = KelompokDriver::find($data_id[0]);
  150. if ($query) {
  151. /**cek apakah id data benar" ada di DB ? */
  152. if ($validator->passes()) {
  153. /**validatornya */
  154. DB::beginTransaction();
  155. $file = $request->file('logo');
  156. if($file){
  157. $nama_file = time()."_".$file->getClientOriginalName();
  158. Storage::putFileAs('public/images/kelompok_driver/', $file, $nama_file );
  159. Storage::delete('public/images/kelompok_driver/'.$query->logo);
  160. $query->logo = $nama_file;
  161. }
  162. $query->nama = $request->nama;
  163. $query->website = $request->website;
  164. $query->deskripsi = $request->deskripsi;
  165. $query->save();
  166. if ($query) {
  167. DB::commit();
  168. $message = 'Berhasil';
  169. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  170. } else {
  171. DB::rollback();
  172. $message = 'Gagal';
  173. return redirect()->back()->with('error', Helper::parsing_alert($message));
  174. }
  175. }
  176. $message = Helper::parsing_alert($validator->errors()->all());
  177. return redirect()->back()->with('error', $message);
  178. } else {
  179. $message = 'Id tdak ditemukan';
  180. return redirect()->back()->with('error', $message);
  181. }
  182. } else {
  183. $message = 'Id tidak boleh kosong';
  184. return redirect()->back()->with('error', $message);
  185. }
  186. }
  187. public function delete_action($id_modelmu)
  188. {
  189. $id = Hashids::decode($id_modelmu);
  190. /**cek idnya apakah ada ? */
  191. if (!empty($id)) {
  192. $cek_data = KelompokDriver::find($id[0]);
  193. if ($cek_data) {
  194. DB::beginTransaction();
  195. try {
  196. Storage::delete('public/images/kelompok_driver/'.$cek_data->logo);
  197. $delete = $cek_data->delete();
  198. DB::commit();
  199. $message = 'Sukses';
  200. $response = [
  201. 'message' => $message,
  202. 'status' => true,
  203. ];
  204. return response()->json($response);
  205. } catch (Exception $e) {
  206. DB::rollback();
  207. $message = 'Gagal';
  208. $response = [
  209. 'message' => $message,
  210. 'status' => false,
  211. ];
  212. return response()->json($response);
  213. }
  214. }
  215. $message = 'Id tidak ditemukan atau sudah dihapus';
  216. $response = [
  217. 'message' => $message,
  218. 'status' => false,
  219. ];
  220. return response()->json($response);
  221. }
  222. $message = 'Id tdak ditemukan';
  223. $response = [
  224. 'message' => $message,
  225. 'status' => false,
  226. ];
  227. return response()->json($response);
  228. }
  229. }