Skip to content

Commit d98f7e8

Browse files
committed
Fixed spelling of "nganh", add basic layout, add API user authentication
1 parent a9d61d3 commit d98f7e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+794
-472
lines changed

app/Http/Controllers/LopController.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
use Illuminate\Http\Request;
55
use App\Models\Lop;
6-
use App\Models\Nghanh;
6+
use App\Models\Nganh;
77

88
class LopController extends Controller
99
{
1010
// views
1111

1212
public function index()
1313
{
14-
$lops = Lop::with('nghanh')->select('id', 'maLop', 'tenLop', 'nghanh_id')->get();
14+
$lops = Lop::with('nganh')->select('id', 'maLop', 'tenLop', 'nganh_id')->get();
1515
return view('lop.index', compact('lops'));
1616
}
1717

1818
public function search(Request $request)
1919
{
20-
$query = Lop::with('nghanh')->select('id', 'maLop', 'tenLop', 'nghanh_id');
20+
$query = Lop::with('nganh')->select('id', 'maLop', 'tenLop', 'nganh_id');
2121

2222
if ($request->has('search')) {
2323
$search = $request->input('search');
@@ -31,16 +31,16 @@ public function search(Request $request)
3131

3232
public function create()
3333
{
34-
$nghanhs = Nghanh::select('id', 'tenNghanh')->get();
35-
return view('lop.create', compact('nghanhs'));
34+
$nganhs = Nganh::select('id', 'tenNganh')->get();
35+
return view('lop.create', compact('nganhs'));
3636
}
3737

3838
public function store(Request $request)
3939
{
4040
$request->validate([
4141
'maLop' => 'required|string|max:10',
4242
'tenLop' => 'required|string|max:255',
43-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
43+
'nganh_id' => 'required|exists:tb_Nganh,id',
4444
]);
4545

4646
Lop::create($request->all());
@@ -50,10 +50,10 @@ public function store(Request $request)
5050
public function edit($id)
5151
{
5252
$lop = Lop::find($id);
53-
$nghanhs = Nghanh::select('id', 'tenNghanh')->get();
53+
$nganhs = Nganh::select('id', 'tenNganh')->get();
5454

5555
if ($lop) {
56-
return view('lop.edit', compact('lop', 'nghanhs'));
56+
return view('lop.edit', compact('lop', 'nganhs'));
5757
}
5858
return redirect()->route('lop.index')->with('error', 'Lop not found');
5959
}
@@ -63,7 +63,7 @@ public function update(Request $request, $id)
6363
$request->validate([
6464
'maLop' => 'required|string|max:10',
6565
'tenLop' => 'required|string|max:255',
66-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
66+
'nganh_id' => 'required|exists:tb_Nganh,id',
6767
]);
6868

6969
$lop = Lop::find($id);
@@ -88,7 +88,7 @@ public function destroy($id)
8888

8989
public function apiIndex(Request $request)
9090
{
91-
$query = Lop::with('nghanh')->select('id', 'maLop', 'tenLop', 'nghanh_id');
91+
$query = Lop::with('nganh')->select('id', 'maLop', 'tenLop', 'nganh_id');
9292

9393
if ($request->has('search')) {
9494
$search = $request->input('search');
@@ -102,7 +102,7 @@ public function apiIndex(Request $request)
102102

103103
public function apiShow($id)
104104
{
105-
$lop = Lop::with('nghanh')->select('id', 'maLop', 'tenLop', 'nghanh_id')->find($id);
105+
$lop = Lop::with('nganh')->select('id', 'maLop', 'tenLop', 'nganh_id')->find($id);
106106
if ($lop) {
107107
return response()->json($lop, 200);
108108
}
@@ -114,7 +114,7 @@ public function apiStore(Request $request)
114114
$request->validate([
115115
'maLop' => 'required|string|max:10',
116116
'tenLop' => 'required|string|max:255',
117-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
117+
'nganh_id' => 'required|exists:tb_Nganh,id',
118118
]);
119119

120120
$lop = Lop::create($request->all());
@@ -126,7 +126,7 @@ public function apiUpdate(Request $request, $id)
126126
$request->validate([
127127
'maLop' => 'required|string|max:10',
128128
'tenLop' => 'required|string|max:255',
129-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
129+
'nganh_id' => 'required|exists:tb_Nganh,id',
130130
]);
131131

132132
$lop = Lop::find($id);
@@ -149,7 +149,7 @@ public function apiDestroy($id)
149149

150150
public function apiSearch(Request $request)
151151
{
152-
$query = Lop::with('nghanh')->select('id', 'maLop', 'tenLop', 'nghanh_id');
152+
$query = Lop::with('nganh')->select('id', 'maLop', 'tenLop', 'nganh_id');
153153

154154
if ($request->has('search')) {
155155
$search = $request->input('search');

app/Http/Controllers/MonHocController.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
use Illuminate\Http\Request;
55
use App\Models\MonHoc;
6-
use App\Models\Nghanh;
6+
use App\Models\Nganh;
77

88
class MonHocController extends Controller
99
{
1010
// Views
1111

1212
public function index()
1313
{
14-
$monHocs = MonHoc::with('nghanh:id,tenNghanh')->select('id', 'maMonHoc', 'tenMonHoc', 'nghanh_id')->get();
14+
$monHocs = MonHoc::with('nganh:id,tenNganh')->select('id', 'maMonHoc', 'tenMonHoc', 'nganh_id')->get();
1515
return view('monhoc.index', compact('monHocs'));
1616
}
1717

1818
public function search(Request $request)
1919
{
20-
$query = MonHoc::with('nghanh:id,tenNghanh')->select('id', 'maMonHoc', 'tenMonHoc', 'nghanh_id');
20+
$query = MonHoc::with('nganh:id,tenNganh')->select('id', 'maMonHoc', 'tenMonHoc', 'nganh_id');
2121

2222
if ($request->has('search')) {
2323
$search = $request->input('search');
@@ -31,16 +31,16 @@ public function search(Request $request)
3131

3232
public function create()
3333
{
34-
$nghanhs = Nghanh::select('id', 'tenNghanh')->get();
35-
return view('monhoc.create', compact('nghanhs'));
34+
$nganhs = Nganh::select('id', 'tenNganh')->get();
35+
return view('monhoc.create', compact('nganhs'));
3636
}
3737

3838
public function store(Request $request)
3939
{
4040
$request->validate([
4141
'maMonHoc' => 'required|string|max:10',
4242
'tenMonHoc' => 'required|string|max:255',
43-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
43+
'nganh_id' => 'required|exists:tb_Nganh,id',
4444
]);
4545

4646
MonHoc::create($request->all());
@@ -50,10 +50,10 @@ public function store(Request $request)
5050
public function edit($id)
5151
{
5252
$monHoc = MonHoc::find($id);
53-
$nghanhs = Nghanh::select('id', 'tenNghanh')->get();
53+
$nganhs = Nganh::select('id', 'tenNganh')->get();
5454

5555
if ($monHoc) {
56-
return view('monhoc.edit', compact('monHoc', 'nghanhs'));
56+
return view('monhoc.edit', compact('monHoc', 'nganhs'));
5757
}
5858
return redirect()->route('monhoc.index')->with('error', 'Môn học không tồn tại');
5959
}
@@ -63,7 +63,7 @@ public function update(Request $request, $id)
6363
$request->validate([
6464
'maMonHoc' => 'required|string|max:10',
6565
'tenMonHoc' => 'required|string|max:255',
66-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
66+
'nganh_id' => 'required|exists:tb_Nganh,id',
6767
]);
6868

6969
$monHoc = MonHoc::find($id);
@@ -88,7 +88,7 @@ public function destroy($id)
8888

8989
public function apiIndex(Request $request)
9090
{
91-
$query = MonHoc::select('id', 'maMonHoc', 'tenMonHoc', 'nghanh_id');
91+
$query = MonHoc::select('id', 'maMonHoc', 'tenMonHoc', 'nganh_id');
9292

9393
if ($request->has('search')) {
9494
$search = $request->input('search');
@@ -102,7 +102,7 @@ public function apiIndex(Request $request)
102102

103103
public function apiShow($id)
104104
{
105-
$monHoc = MonHoc::select('id', 'maMonHoc', 'tenMonHoc', 'nghanh_id')->find($id);
105+
$monHoc = MonHoc::select('id', 'maMonHoc', 'tenMonHoc', 'nganh_id')->find($id);
106106
if ($monHoc) {
107107
return response()->json($monHoc, 200);
108108
}
@@ -114,7 +114,7 @@ public function apiStore(Request $request)
114114
$request->validate([
115115
'maMonHoc' => 'required|string|max:10',
116116
'tenMonHoc' => 'required|string|max:255',
117-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
117+
'nganh_id' => 'required|exists:tb_Nganh,id',
118118
]);
119119

120120
$monHoc = MonHoc::create($request->all());
@@ -126,7 +126,7 @@ public function apiUpdate(Request $request, $id)
126126
$request->validate([
127127
'maMonHoc' => 'required|string|max:10',
128128
'tenMonHoc' => 'required|string|max:255',
129-
'nghanh_id' => 'required|exists:tb_Nghanh,id',
129+
'nganh_id' => 'required|exists:tb_Nganh,id',
130130
]);
131131

132132
$monHoc = MonHoc::find($id);
+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Models\Nganh;
7+
use App\Models\Khoa;
8+
9+
class NganhController extends Controller
10+
{
11+
// Web Methods
12+
13+
public function index()
14+
{
15+
$nganhs = Nganh::with('khoa')->select('id', 'maNganh', 'tenNganh', 'khoa_id')->get();
16+
return view('nganh.index', compact('nganhs'));
17+
}
18+
19+
public function search(Request $request)
20+
{
21+
$query = Nganh::with('khoa')->select('id', 'maNganh', 'tenNganh', 'khoa_id');
22+
23+
// Thêm điều kiện tìm kiếm
24+
if ($request->has('search')) {
25+
$search = $request->input('search');
26+
$query->where('maNganh', 'LIKE', "%$search%")
27+
->orWhere('tenNganh', 'LIKE', "%$search%");
28+
}
29+
30+
$nganhs = $query->get();
31+
return view('nganh.index', compact('nganhs'));
32+
}
33+
34+
public function create()
35+
{
36+
$khoas = Khoa::select('id', 'tenKhoa')->get(); // Lấy danh sách các Khoa
37+
return view('nganh.create', compact('khoas'));
38+
}
39+
40+
public function store(Request $request)
41+
{
42+
$request->validate([
43+
'maNganh' => 'required|string|max:10',
44+
'tenNganh' => 'required|string|max:255',
45+
'khoa_id' => 'required|exists:tb_Khoa,id', // Kiểm tra khóa ngoại
46+
]);
47+
48+
Nganh::create($request->all());
49+
return redirect()->route('nganh.index')->with('success', 'Ngành added successfully');
50+
}
51+
52+
public function edit($id)
53+
{
54+
$nganh = Nganh::find($id);
55+
if ($nganh) {
56+
$khoas = Khoa::select('id', 'tenKhoa')->get(); // Lấy danh sách các Khoa
57+
return view('nganh.edit', compact('nganh', 'khoas'));
58+
}
59+
return redirect()->route('nganh.index')->with('error', 'Ngành not found');
60+
}
61+
62+
public function update(Request $request, $id)
63+
{
64+
$request->validate([
65+
'maNganh' => 'required|string|max:10',
66+
'tenNganh' => 'required|string|max:255',
67+
'khoa_id' => 'required|exists:tb_Khoa,id', // Kiểm tra khóa ngoại
68+
]);
69+
70+
$nganh = Nganh::find($id);
71+
if ($nganh) {
72+
$nganh->update($request->all());
73+
return redirect()->route('nganh.index')->with('success', 'Ngành updated successfully');
74+
}
75+
return redirect()->route('nganh.index')->with('error', 'Ngành not found');
76+
}
77+
78+
public function destroy($id)
79+
{
80+
$nganh = Nganh::find($id);
81+
if ($nganh) {
82+
$nganh->delete();
83+
return redirect()->route('nganh.index')->with('success', 'Ngành deleted successfully');
84+
}
85+
return redirect()->route('nganh.index')->with('error', 'Ngành not found');
86+
}
87+
88+
// API JSON Methods
89+
90+
public function apiIndex(Request $request)
91+
{
92+
$query = Nganh::with('khoa')->select('id', 'maNganh', 'tenNganh', 'khoa_id');
93+
94+
// Thêm điều kiện tìm kiếm
95+
if ($request->has('search')) {
96+
$search = $request->input('search');
97+
$query->where('maNganh', 'LIKE', "%$search%")
98+
->orWhere('tenNganh', 'LIKE', "%$search%");
99+
}
100+
101+
$nganhs = $query->get();
102+
return response()->json($nganhs, 200);
103+
}
104+
105+
public function apiShow($id)
106+
{
107+
$nganh = Nganh::with('khoa')->select('id', 'maNganh', 'tenNganh', 'khoa_id')->find($id);
108+
if ($nganh) {
109+
return response()->json($nganh, 200);
110+
}
111+
return response()->json(['message' => 'Not found'], 404);
112+
}
113+
114+
public function apiStore(Request $request)
115+
{
116+
$request->validate([
117+
'maNganh' => 'required|string|max:10',
118+
'tenNganh' => 'required|string|max:255',
119+
'khoa_id' => 'required|exists:tb_Khoa,id', // Kiểm tra khóa ngoại
120+
]);
121+
122+
$nganh = Nganh::create($request->all());
123+
return response()->json($nganh, 201);
124+
}
125+
126+
public function apiUpdate(Request $request, $id)
127+
{
128+
$request->validate([
129+
'maNganh' => 'required|string|max:10',
130+
'tenNganh' => 'required|string|max:255',
131+
'khoa_id' => 'required|exists:tb_Khoa,id', // Kiểm tra khóa ngoại
132+
]);
133+
134+
$nganh = Nganh::find($id);
135+
if ($nganh) {
136+
$nganh->update($request->all());
137+
return response()->json($nganh, 200);
138+
}
139+
return response()->json(['message' => 'Not found'], 404);
140+
}
141+
142+
public function apiDestroy($id)
143+
{
144+
$nganh = Nganh::find($id);
145+
if ($nganh) {
146+
$nganh->delete();
147+
return response()->json(['message' => 'Ngành deleted'], 200);
148+
}
149+
return response()->json(['message' => 'Not found'], 404);
150+
}
151+
152+
public function apiSearch(Request $request)
153+
{
154+
$query = Nganh::with('khoa')->select('id', 'maNganh', 'tenNganh', 'khoa_id');
155+
156+
// Thêm điều kiện tìm kiếm
157+
if ($request->has('search')) {
158+
$search = $request->input('search');
159+
$query->where('maNganh', 'LIKE', "%$search%")
160+
->orWhere('tenNganh', 'LIKE', "%$search%");
161+
}
162+
163+
$nganhs = $query->get();
164+
return response()->json($nganhs, 200);
165+
}
166+
}

0 commit comments

Comments
 (0)