first(); // dd($lastDocument->admission_id); return view('admin.documents.create', compact('admissions', 'lastDocument')); } public function store(StoreDocumentRequest $request): RedirectResponse { $this->saveFile($request); return redirect()->route('documents.index'); } public function edit(Document $document) { $admissions = Admission::pluck('name', 'id'); return view('admin.documents.edit', compact('admissions', 'document')); } public function show(Document $document): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { return view('admin.documents.show', compact('document')); } public function update(UpdateDocumentRequest $request, Document $document): RedirectResponse { $validated = $request->validated(); $document->name = $validated['name']; $document->description = $validated['description']; $document->position = $validated['position']; $document->admission_id = $validated['admission_id']; $document->save(); return redirect()->route('documents.index'); } public function destroy(Document $document) { $document->delete(); return redirect()->route('documents.index'); } public function download($id) { $file = (new Document())->find($id); return Storage::url($file->url); } public function createFromAdmission(Admission $admission): View { $admissions = Admission::pluck('name', 'id'); $documents = Document::where('admission_id', '=', $admission->id)->get(); return view('admin.documents.create_from_admission', compact('admissions', 'admission', 'documents')); } public function storeFromAdmission(StoreDocumentRequest $request): RedirectResponse { $this->saveFile($request); return redirect()->route('admissions.index'); } private function saveFile(StoreDocumentRequest $request) { $fileName = $request->file('document')->getClientOriginalName(); $name = Storage::put('public', $request->file('document')); $validated = $request->validated(); $document = new Document(); $document->name = $validated['name']; $document->description = $validated['description']; $document->file_name = $fileName; $document->url = Storage::url($name); $document->position = $validated['position']; $document->admission_id = $validated['admission_id']; $document->save(); } }