# DepartmentController
app / Http / Controllers / DepartmentController.php
# index
public function index()
{
if (Auth::user()->can('View Department')){
$departments = Department::all();
return view('Department.index', ['departments' => $departments]);
}else{
return abort('404');
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# serverSideData
public function serverSideDatas(){
if (!Auth::user()->can('View Department')){
return abort('404');
}
$departments = Department::query();
return Datatables::of($departments)
->addColumn('action', function ($each){
$edit = '';
$delete= '';
if (Auth::user()->can('Edit Department')){
$edit = '<a href="'. route('department.edit', $each->id) .'" class="btn btn-outline-warning me-2"><i class="fa-solid fa-pen-to-square"></i></a>';
}
if (Auth::user()->can('Delete Department')){
$delete = '<a href="#" class="btn btn-outline-danger delete-btn" data-id="'.$each->id.'" data-name="'.$each->title.'"><i class="fa-solid fa-trash-alt"></i></a>';
}
$detail = '<a href="'. route('department.show', $each->id) .'" class="btn btn-outline-primary me-2"><i class="fa-solid fa-circle-info"></i></a>';
return '<div class="d-flex">'. $detail. $edit . $delete.'</div>';
})
->addColumn('plus_icon', function ($each){
return null;
})
->rawColumns(['action']) // for HTML return
->make(true);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
သတိပြုရန်
DataTable အတွက် serverSideData ထုတ်တဲ့နေရာမှာ
$departments = Department::all()->get();
ဆိုပြီးမထုတ်ရပါ။ get() က data တွေအကုန်ဆွဲထုတ်လာလို့ Database Performance ကျစေပါတယ်။ အောက်ဖော်ပြပါ အတိုင်းထုတ်ပါ။
$departments = Department::query();
1
HTML Tag တွေကိုဘယ်လို return ပြန်မလဲ ?
HTML Tag တွေပါရောပြီး return ပြန်မယ်ဆိုရင် rawColumns([]) ထဲမှာ register လုပ်ပေးရမယ်။
-> rawColumns(['action'])
1
# create
public function create()
{
if (!Auth::user()->can('Create Department')){
return abort('404');
}
return view('Department.create');
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# store
public function store(StoreDepartmentRequest $request)
{
if (!Auth::user()->can('Create Department')){
return abort('404');
}
$department = new Department();
$department->title = $request->title;
$department->save();
return redirect()
->route('department.index')
->with('create', $department->title.' is successfully created.');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# show
public function show(Department $department)
{
if (!Auth::user()->can('View Department')){
return abort('404');
}
}
1
2
3
4
5
6
2
3
4
5
6
# edit
public function edit(Department $department)
{
if (!Auth::user()->can('Edit Department')){
return abort('404');
}
return view('Department.edt', ['department' => $department]);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# update
public function update(UpdateDepartmentRequest $request, Department $department)
{
if (!Auth::user()->can('Edit Department')){
return abort('404');
}
$department->title = $request->title;
$department->update();
return redirect()
->route('department.index')
->with('update', $department->title.' is successfully updated.');
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# destroy
public function destroy(Department $department)
{
if (!Auth::user()->can('Delete Department')){
return abort('404');
}
$department->delete();
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Code Overview
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Models\Department;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\DataTables;
class DepartmentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (Auth::user()->can('View Department')){
$departments = Department::all();
return view('Department.index', ['departments' => $departments]);
}else{
return abort('404');
}
}
public function serverSideDatas(){
if (!Auth::user()->can('View Department')){
return abort('404');
}
$departments = Department::query();
return Datatables::of($departments)
->addColumn('action', function ($each){
$edit = '';
$delete= '';
if (Auth::user()->can('Edit Department')){
$edit = '<a href="'. route('department.edit', $each->id) .'" class="btn btn-outline-warning me-2"><i class="fa-solid fa-pen-to-square"></i></a>';
}
if (Auth::user()->can('Delete Department')){
$delete = '<a href="#" class="btn btn-outline-danger delete-btn" data-id="'.$each->id.'" data-name="'.$each->title.'"><i class="fa-solid fa-trash-alt"></i></a>';
}
$detail = '<a href="'. route('department.show', $each->id) .'" class="btn btn-outline-primary me-2"><i class="fa-solid fa-circle-info"></i></a>';
return '<div class="d-flex">'. $detail. $edit . $delete.'</div>';
})
->addColumn('plus_icon', function ($each){
return null;
})
->rawColumns(['action']) // for HTML return
->make(true);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
if (!Auth::user()->can('Create Department')){
return abort('404');
}
return view('Department.create');
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreDepartmentRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreDepartmentRequest $request)
{
if (!Auth::user()->can('Create Department')){
return abort('404');
}
$department = new Department();
$department->title = $request->title;
$department->save();
return redirect()
->route('department.index')
->with('create', $department->title.' is successfully created.');
}
/**
* Display the specified resource.
*
* @param \App\Models\Department $department
* @return \Illuminate\Http\Response
*/
public function show(Department $department)
{
if (!Auth::user()->can('View Department')){
return abort('404');
}
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Department $department
* @return \Illuminate\Http\Response
*/
public function edit(Department $department)
{
if (!Auth::user()->can('Edit Department')){
return abort('404');
}
return view('Department.edt', ['department' => $department]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateDepartmentRequest $request
* @param \App\Models\Department $department
* @return \Illuminate\Http\Response
*/
public function update(UpdateDepartmentRequest $request, Department $department)
{
if (!Auth::user()->can('Edit Department')){
return abort('404');
}
$department->title = $request->title;
$department->update();
return redirect()
->route('department.index')
->with('update', $department->title.' is successfully updated.');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Department $department
* @return \Illuminate\Http\Response
*/
public function destroy(Department $department)
{
if (!Auth::user()->can('Delete Department')){
return abort('404');
}
$department->delete();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148