documentService = $documentService; } public function index() { $title = 'Upload Documents'; $countryOptions = Country::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Country:$id" => "Country - $title"]); $serviceOptions = Service::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Service:$id" => "Service - $title"]); $testOptions = Test::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Test:$id" => "Test - $title"]); $modelOptions = $countryOptions->merge($serviceOptions)->merge($testOptions); return view('document::document.index', compact('modelOptions', 'title')); } public function destroy($id) { try { $document = Document::findOrFail($id); if (File::exists('storage/' . $document->document_path)) { File::delete('storage/' . $document->document_path); } $document->delete(); session()->flash('Document Deleted'); return response()->json(['status' => true, 'msg' => 'Document Deleted']); } catch (\Throwable $th) { session()->flash('error', $th->getMessage()); } } public function uploadDocument(Request $request) { $isNumeric = is_numeric($request->title); $validator = Validator::make($request->all(), [ 'title' => $isNumeric ? 'required|exists:required_documents,id' : 'required|string', 'model' => 'required', 'document' => 'required|array', ], [ 'title.required' => 'Document title is required!', 'title.exists' => 'Document does not exists in record!', 'document' => 'Please upload document first!', 'model' => 'Please Select Category' ]); if ($validator->fails()) { return response()->json(['status' => 422, 'errors' => $validator->errors()], 422); } try { $modelValues = explode(':', $request->model); $modelClass = "Modules\\CCMS\\Models\\{$modelValues[0]}"; $model = $modelClass::findOrFail($modelValues[1]); foreach ($request->input('document', []) as $file) { $model->addToDocumentCollection(collectionName: 'uploads/document', file: $file, documentName: $request->title, referenceDocumentId: null); } return response()->json([ 'status' => true, 'msg' => 'Document has been uploaded!', // 'view' => view('student::student.section.document.table', [ // 'student' => $student, // ])->render(), ], 200); } catch (\Throwable $th) { return response()->json([ 'status' => false, 'msg' => $th->getMessage(), ], 500); } } public function store(Request $request) { $path = storage_path('tmp/uploads'); if (!file_exists($path)) { mkdir($path, 0777, true); } $file = $request->file('file'); $name = uniqid() . '_' . trim($file->getClientOriginalName()); $file->move($path, $name); return response()->json([ 'name' => $name, 'original_name' => $file->getClientOriginalName(), ]); } public function getAllDocuments() { $model = Document::query()->latest(); return DataTables::eloquent($model) ->setRowClass('tableRow') ->addColumn('name', function (Document $document) { $extension = $document->getExtension(); $assetUrl = $document->getUrl(); $html = $document->isImageFile() ? "
" : ""; return "