with([ "departments" => Department::all(), ]); } public function create() { return view("dashboard.departments.create"); } 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; $path = '/Departments/' . $request->code; $department->location = $path; $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 store(NewDepartmentRequest $request) { $department = new Department(); $department->name = $request->name; $department->code = $request->code; if(!Storage::disk('local')->has('Departments/' . $request->code)){ Storage::disk('local')->makeDirectory('Departments/' . $request->code); } $department->user_id = auth()->id(); $department->location = '/Departments/' . $request->code; dd($department->location); $department->save(); Alert::flash("New Department added successfully"); return redirect()->route("dashboard.departments.index"); } public function destroy(Request $request, $id) { $department = Department::find($id); $department->delete(); Alert::flash($department->name . " deleted successfully"); return redirect()->route("dashboard.departments.index"); } }