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
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Resources\FormatApi;
use App\Models\Agama44;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class Agama44Controller extends Controller
{
public function getAgama44(Request $request)
{
$agama = Agama44::all();
return new FormatApi(true, 'Berhasil mendapatkan data agama', $agama);
}
public function getDetailAgama44(Request $request, $id)
{
$agama = Agama44::find($id);
if (!$agama) {
return new FormatApi(false, 'Agama tidak ditemukan', null);
}
return new FormatApi(true, 'Berhasil mendapatkan data agama', $agama);
}
public function postAgama44(Request $request)
{
$validator = Validator::make($request->all(), [
'nama_agama' => 'required',
]);
if ($validator->fails()) {
return new FormatApi(false, 'Validasi gagal', $validator->errors()->all());
}
$createUser = Agama44::create([
'nama_agama' => $request->nama_agama,
]);
if ($createUser) {
return new FormatApi(true, 'Berhasil menambahkan data agama', $createUser);
} else {
return new FormatApi(false, 'Gagal menambahkan data agama', null);
}
}
public function putAgama44(Request $request, $id)
{
$agama = Agama44::find($id);
if (!$agama) {
return new FormatApi(false, 'Agama tidak ditemukan', null);
}
$validator = Validator::make($request->all(), [
'nama_agama' => 'required',
]);
if ($validator->fails()) {
return new FormatApi(false, 'Validasi gagal', $validator->errors()->all());
}
$agama->update([
'nama_agama' => $request->nama_agama,
]);
return new FormatApi(true, 'Berhasil mengubah data agama', null);
}
public function deleteAgama44(Request $request, $id)
{
$agama = Agama44::find($id);
if (!$agama) {
return new FormatApi(false, 'Agama tidak ditemukan', null);
}
$agama->delete();
return new FormatApi(true, 'Berhasil menghapus data agama', null);
}
}