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;
|
---|
13 |
|
---|
14 | class DepartmentsController extends Controller
|
---|
15 | {
|
---|
16 | public function index()
|
---|
17 | {
|
---|
18 | return view("dashboard.departments.index")->with([
|
---|
19 | "departments" => Department::all()
|
---|
20 | ]);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public function create()
|
---|
24 | {
|
---|
25 | return view("dashboard.departments.create");
|
---|
26 | }
|
---|
27 |
|
---|
28 | public function editShow($id)
|
---|
29 | {
|
---|
30 | return view("dashboard.departments.edit")->with([
|
---|
31 | "department" => Department::findOrFail($id)
|
---|
32 | ]);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public function edit(UpdateDepartmentRequest $request, $id)
|
---|
36 | {
|
---|
37 | $department = Department::findOrFail($id);
|
---|
38 |
|
---|
39 | $department->name = $request->name;
|
---|
40 | $department->code = $request->code;
|
---|
41 |
|
---|
42 | $department->save();
|
---|
43 |
|
---|
44 | Alert::flash("Department edited successfully");
|
---|
45 |
|
---|
46 | return redirect()->route("dashboard.departments.index");
|
---|
47 | }
|
---|
48 |
|
---|
49 | public function store(NewDepartmentRequest $request)
|
---|
50 | {
|
---|
51 | $department = new Department();
|
---|
52 |
|
---|
53 | $department->name = $request->name;
|
---|
54 | $department->code = $request->code;
|
---|
55 |
|
---|
56 | $department->user_id = auth()->id();
|
---|
57 |
|
---|
58 | $department->save();
|
---|
59 |
|
---|
60 | Alert::flash("New Department added successfully");
|
---|
61 |
|
---|
62 | return redirect()->route("dashboard.departments.index");
|
---|
63 | }
|
---|
64 |
|
---|
65 | public function destroy(Request $request, $id)
|
---|
66 | {
|
---|
67 | $department = Department::find($id);
|
---|
68 |
|
---|
69 | $department->delete();
|
---|
70 |
|
---|
71 | Alert::flash($department->name . " deleted successfully");
|
---|
72 |
|
---|
73 | return redirect()->route("dashboard.departments.index");
|
---|
74 | }
|
---|
75 | }
|
---|