source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 120759b

develop
Last change on this file since 120759b was 120759b, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added UserFactory, edited html, added delete to departments

  • Property mode set to 100644
File size: 2.5 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewDepartmentRequest;
7use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
8use App\Models\Department;
9use App\Models\User;
10use Illuminate\Http\Request;
11use App\Http\Controllers\Controller;
12use Illuminate\Support\Facades\Auth;
13use Illuminate\Support\Facades\Storage;
14
15class DepartmentsController extends Controller
16{
17 public function index()
18 {
19 return view("dashboard.departments.index")->with([
20 "departments" => Department::all(),
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 store(NewDepartmentRequest $request)
37 {
38 $department = new Department();
39
40 $department->name = $request->name;
41 $department->code = $request->code;
42
43 if(!Storage::disk('local')->has('Departments/' . $request->code)){
44 Storage::disk('local')->makeDirectory('Departments/' . $request->code);
45 }
46
47 $department->user_id = auth()->id();
48 $department->location = '/Departments/' . $request->code;
49
50 $department->save();
51
52 Alert::flash("New Department added successfully");
53
54 return redirect()->route("dashboard.departments.index");
55 }
56
57 public function edit(UpdateDepartmentRequest $request, $id)
58 {
59 $department = Department::findOrFail($id);
60
61 $oldDepartmentCode = $department->code;
62
63 $department->name = $request->name;
64 $department->code = $request->code;
65
66 $path = '/Departments/' . $request->code;
67 $department->location = $path;
68
69 $files = Storage::allFiles($oldDepartmentCode);
70
71 if($department->isDirty('code'))
72 {
73 if(!Storage::disk('local')->has('Departments/' . $request->code)){
74 Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code);
75 }
76 }
77
78 $department->save();
79
80 Alert::flash("Department edited successfully");
81
82 return redirect()->route("dashboard.departments.index");
83 }
84
85 public function destroy(Request $request, $id)
86 {
87 $department = Department::find($id);
88
89 $department->delete();
90
91 Alert::flash($department->name . " deleted successfully");
92
93 return redirect()->route("dashboard.departments.index");
94 }
95}
Note: See TracBrowser for help on using the repository browser.