with([ "departments" => Department::all(), ]); } public function create() { return view("dashboard.departments.create"); } public function store(NewDepartmentRequest $request) { $department = new Department(); $department->name = $request->name; $department->code = $request->code; $len = Storage::disk('local')->path(''); if(!Storage::disk('local')->has('Departments/' . $request->code)){ Storage::disk('local')->makeDirectory('Departments/' . $request->code); } $department->user_id = auth()->id(); $department->location = Storage::disk('local')->path('') . 'Departments/' . $request->code; $department->save(); Alert::flash("New Department added successfully"); return redirect()->route("dashboard.departments.index"); } public function editShow($id) { return view("dashboard.departments.edit")->with([ "department" => Department::findOrFail($id) ]); } public function edit(UpdateDepartmentRequest $request, $id) { $department = Department::findOrFail($id); $oldDepartmentCode = $department->code; $department->name = $request->name; $department->code = $request->code; $department->location = Storage::disk('local')->path('') . 'Departments/' . $request->code; $files = Storage::allFiles($oldDepartmentCode); if($department->isDirty('code')) { if(!Storage::disk('local')->has('Departments/' . $request->code)){ Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code); } } $department->save(); Alert::flash("Department edited successfully"); return redirect()->route("dashboard.departments.index"); } public function destroy($id) { $department = Department::find($id); //$department->delete(); $documents = $department->document()->count(); if($documents > 0){ Alert::flash($department->name . " has " . $documents . " document/s associated", "error"); } else { $department->delete(); Alert::flash($department->name . " deleted successfully"); } return redirect()->route("dashboard.departments.index"); } }