[194a359] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Helpers\Alert;
|
---|
| 6 | use App\Http\Requests\Dashboard\NewDepartmentRequest;
|
---|
| 7 | use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
|
---|
| 8 | use App\Models\Department;
|
---|
| 9 | use App\Models\User;
|
---|
| 10 | use Illuminate\Http\Request;
|
---|
| 11 | use App\Http\Controllers\Controller;
|
---|
| 12 | use Illuminate\Support\Facades\Auth;
|
---|
[24a616f] | 13 | use Illuminate\Support\Facades\Storage;
|
---|
[194a359] | 14 |
|
---|
| 15 | class DepartmentsController extends Controller
|
---|
| 16 | {
|
---|
| 17 | public function index()
|
---|
| 18 | {
|
---|
| 19 | return view("dashboard.departments.index")->with([
|
---|
[24a616f] | 20 | "departments" => Department::all(),
|
---|
[194a359] | 21 | ]);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public function create()
|
---|
| 25 | {
|
---|
| 26 | return view("dashboard.departments.create");
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public function editShow($id)
|
---|
| 30 | {
|
---|
| 31 | return view("dashboard.departments.edit")->with([
|
---|
| 32 | "department" => Department::findOrFail($id)
|
---|
| 33 | ]);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public function edit(UpdateDepartmentRequest $request, $id)
|
---|
| 37 | {
|
---|
| 38 | $department = Department::findOrFail($id);
|
---|
| 39 |
|
---|
[24a616f] | 40 | $oldDepartmentCode = $department->code;
|
---|
| 41 |
|
---|
[194a359] | 42 | $department->name = $request->name;
|
---|
| 43 | $department->code = $request->code;
|
---|
| 44 |
|
---|
[24a616f] | 45 | $path = '/Departments/' . $request->code;
|
---|
| 46 | $department->location = $path;
|
---|
| 47 |
|
---|
| 48 | $files = Storage::allFiles($oldDepartmentCode);
|
---|
| 49 |
|
---|
| 50 | if($department->isDirty('code'))
|
---|
| 51 | {
|
---|
| 52 | if(!Storage::disk('local')->has('Departments/' . $request->code)){
|
---|
| 53 | Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[194a359] | 57 | $department->save();
|
---|
| 58 |
|
---|
| 59 | Alert::flash("Department edited successfully");
|
---|
| 60 |
|
---|
| 61 | return redirect()->route("dashboard.departments.index");
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public function store(NewDepartmentRequest $request)
|
---|
| 65 | {
|
---|
| 66 | $department = new Department();
|
---|
| 67 |
|
---|
| 68 | $department->name = $request->name;
|
---|
| 69 | $department->code = $request->code;
|
---|
| 70 |
|
---|
[24a616f] | 71 | if(!Storage::disk('local')->has('Departments/' . $request->code)){
|
---|
| 72 | Storage::disk('local')->makeDirectory('Departments/' . $request->code);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[194a359] | 75 | $department->user_id = auth()->id();
|
---|
[24a616f] | 76 | $department->location = '/Departments/' . $request->code;
|
---|
| 77 |
|
---|
| 78 | dd($department->location);
|
---|
[194a359] | 79 |
|
---|
| 80 | $department->save();
|
---|
| 81 |
|
---|
| 82 | Alert::flash("New Department added successfully");
|
---|
| 83 |
|
---|
| 84 | return redirect()->route("dashboard.departments.index");
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public function destroy(Request $request, $id)
|
---|
| 88 | {
|
---|
| 89 | $department = Department::find($id);
|
---|
| 90 |
|
---|
| 91 | $department->delete();
|
---|
| 92 |
|
---|
| 93 | Alert::flash($department->name . " deleted successfully");
|
---|
| 94 |
|
---|
| 95 | return redirect()->route("dashboard.departments.index");
|
---|
| 96 | }
|
---|
| 97 | }
|
---|