'admin.dashboard' | jangan lupa diganti private $header = 'Tips'; private $sub_header = 'Tips'; private $url = 'admin.raharja.tips.'; public function __construct() { DB::enableQueryLog(); /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */ // $this->middleware('permission:searching-list|searching-create|searching-update|searching-delete', ['only' => ['index', 'create', 'update']]); // $this->middleware('permission:searching-create', ['only' => ['create', 'create_action']]); // $this->middleware('permission:searching-update', ['only' => ['update', 'update_action']]); // $this->middleware('permission:searching-delete', ['only' => ['delete']]); } /** * Ini contoh crud yang sudah jalan * index digunakna untuk tampilan awal dari menu yang akan dibuat * Untuk CRUD Biar cepat * Silahkan ganti * EloquentMu:: => dengan model anda * $id_modelmu => ganti dengan id di model anda * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route */ public function index() { Helper::swal(); $data = [ //bawaan 'title' => $this->title, 'route' => $this->route, 'header' => $this->header, 'sub_header' => $this->sub_header, ]; // dd($data); return view($this->url . 'index', $data); } public function getData($udd_id = null) { $query = Tips::query(); return Datatables::of($query) ->addIndexColumn() ->addColumn('count_like', function($query){ return count($query->tipsLike); }) ->addColumn('aksi', function ($query) { /**cek role */ $aksi = ''; // if (Auth::user()->can('master-jenis-kantong-update')) { $aksi .= " Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit"; // } // if (Auth::user()->can('master-jenis-kantong-delete')) { $aksi .= " Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete"; // } return $aksi; }) ->rawColumns(['aksi']) ->toJson(); } public function create() { $data = [ //bawaan 'title' => $this->title, 'route' => $this->route, 'header' => $this->header, 'sub_header' => $this->sub_header, ]; Helper::swal(); return view($this->url . 'create', $data); } public function create_action(Request $request) { //declare post untuk simpan semua request yang diinputkan oleh form name="" $post = $request->all(); $file = $request->file('gambar'); //dd($post); /**digunakan untuk set rule validator */ $rules = [ 'title' => 'required', 'gambar' => 'required|file|max:1000', 'sub_title' => 'required', 'date_publish' => 'required', ]; /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */ $alert = [ 'file' => 'File maksimal 1MB' ]; $validator = Validator::make($post, $rules, $alert); if ($validator->passes()) { /**menggunakan transaction */ DB::beginTransaction(); $nama_file = time()."_".$file->getClientOriginalName(); $tujuan_upload = 'gambar'; //$file->move($tujuan_upload,$nama_file); Storage::putFileAs('public/images/tips/', $file, $nama_file ); $query = new Tips(); $query->title = $request->title; $query->content = Helper::input_summernote($request->content); $query->sub_title = Helper::input_summernote($request->sub_title); $query->date_publish = $request->date_publish; $query->user_id = Auth::user()->id; $query->thumbnail = $nama_file; $query->save(); if ($query) { DB::commit(); $message = 'Berhasil'; return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message)); } else { DB::rollback(); $message = 'Gagal'; return redirect()->back()->with('error', Helper::parsing_alert($message)); } } /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */ $message = Helper::parsing_alert($validator->errors()->all()); return redirect()->back()->with('error', $message)->withInput(); } public function update($data_id) { Helper::swal(); $data_id = Hashids::decode($data_id); if (!empty($data_id)) { $cek_data = Tips::where('id', $data_id[0])->first(); if ($cek_data) { $data = [ //bawaan 'title' => $this->title, 'route' => $this->route, 'header' => $this->header, 'sub_header' => $this->sub_header, 'data' => $cek_data ]; return view($this->url . 'update', $data); } $message = 'Id tidak ditemukan atau sudah dihapus'; return redirect()->back()->with('error', $message); } $message = 'Id tdak ditemukan'; return redirect()->back()->with('error', $message); } public function update_action(Request $request) { $post = $request->all(); /**digunakan untuk set rule validator */ $rules = [ 'title' => 'required', 'content' => 'required', 'sub_title' => 'required', 'date_publish' => 'required', ]; /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */ $alert = []; $validator = Validator::make($post, $rules, $alert); $data_id = Hashids::decode($request['id']); if (!empty($data_id)) { /**cek apakah data_idnya ada ? */ $query = Tips::find($data_id[0]); if ($query) { /**cek apakah id data benar" ada di DB ? */ if ($validator->passes()) { /**validatornya */ DB::beginTransaction(); $file = $request->file('gambar'); if($file){ $nama_file = time()."_".$file->getClientOriginalName(); $tujuan_upload = 'gambar'; //$file->move($tujuan_upload,$nama_file); Storage::putFileAs('public/images/tips/', $file, $nama_file ); //File::delete('gambar/'.$query->gambar); Storage::delete('public/images/tips/'.$query->thumbnail); $query->thumbnail = $nama_file; } $query->title = $request->title; $query->content = Helper::input_summernote($request->content); $query->sub_title = Helper::input_summernote($request->sub_title); $query->date_publish = $request->date_publish; $query->save(); if ($query) { DB::commit(); $message = 'Berhasil'; return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message)); } else { DB::rollback(); $message = 'Gagal'; return redirect()->back()->with('error', Helper::parsing_alert($message)); } } $message = Helper::parsing_alert($validator->errors()->all()); return redirect()->back()->with('error', $message); } else { $message = 'Id tdak ditemukan'; return redirect()->back()->with('error', $message); } } else { $message = 'Id tidak boleh kosong'; return redirect()->back()->with('error', $message); } } public function delete_action($id_modelmu) { $id = Hashids::decode($id_modelmu); /**cek idnya apakah ada ? */ if (!empty($id)) { $cek_data = Tips::find($id[0]); if ($cek_data) { DB::beginTransaction(); try { $arr_likes = $cek_data->tipsLike; foreach($arr_likes as $like){ $like->delete(); } //File::delete('gambar/'.$cek_data->gambar); Storage::delete('public/images/tips/'.$cek_data->thumbnail); $delete = $cek_data->delete(); DB::commit(); $message = 'Sukses'; $response = [ 'message' => $message, 'status' => true, ]; return response()->json($response); } catch (Exception $e) { DB::rollback(); $message = 'Gagal'; $response = [ 'message' => $message, 'status' => false, ]; return response()->json($response); } } $message = 'Id tidak ditemukan atau sudah dihapus'; $response = [ 'message' => $message, 'status' => false, ]; return response()->json($response); } $message = 'Id tdak ditemukan'; $response = [ 'message' => $message, 'status' => false, ]; return response()->json($response); } }