fileService = $fileService; } public static function middleware(): array { return [ //new Middleware('permission:/setting/peraturan'), ]; } public function index(Request $request) { if ($request->ajax()) { $query = $this->fileService->getAll(); $result = datatables()->of($query) ->addColumn('file_upload', function ($row) { if ($row->file_upload) { $storagePath = 'peraturan'; return ' '; } else { return 'No File'; } }) ->addColumn('action', function ($data) { $actionBtn = 'Edit'; $actionBtn .= '
'; $actionBtn .= csrf_field(); $actionBtn .= method_field('DELETE'); $actionBtn .= ''; $actionBtn .= '
'; return $actionBtn; }) ->rawColumns(['file_upload', 'action']) ->make(true); return $result; } return view('setting.file-peraturan.index'); } public function create() { return view('setting.file-peraturan.create'); } public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'file_document' => 'required|file|max:2048|mimes:pdf,doc,docx,xlsx,xls', ], [ 'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.', ]); try { $this->fileService->save($request->all()); return redirect()->route('peraturan.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil diupload.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $data = $this->fileService->find($id); if ($data) { $data['storagePath'] = 'peraturan'; } return view('setting.file-peraturan.edit', compact('data')); } public function update(Request $request, $id) { $request->validate([ 'name' => 'required|string|max:255', 'file_document' => 'nullable|file|max:2048|mimes:pdf,doc,docx,xlsx,xls', ], [ 'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.', ]); try { $data = $request->all(); $data['id'] = $id; $this->fileService->save($data); return redirect()->route('peraturan.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy($id) { try { $data = $this->fileService->find($id); if ($data) { $storagePath = 'peraturan'; } $this->fileService->destroy($cms, $storagePath); return redirect()->route('unit.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } }