rsabhk-joko_prasetio/app/Http/Controllers/ServiceController.php
2025-04-27 22:31:11 +07:00

131 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\helper\helperController;
use App\Models\service;
use Illuminate\Http\Request;
class ServiceController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$data = [
'title' => 'Pelayanan'
];
return view('service.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
try {
$datas= request('data');
foreach ($datas as $value) {
if (!empty($value['name']) && !empty($value['tarif'])) {
$payload = [
'uid' => (new helperController)->getUid(),
'name' => $value['name'],
'tarif' => (int) str_replace('.', '', $value['tarif'])
];
service::create($payload);
}
}
return response()->json([
'status' => 'success',
'message' => 'Data berhasil disimpan.'
], 200);
} catch (\Throwable $th) {
return response()->json([
'status' => 'error',
'message' => 'Terjadi kesalahan saat menyimpan data.',
'error' => $th->getMessage()
], 500);
}
}
/**
* Display the specified resource.
*/
public function show(service $service)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(service $service)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $uid)
{
try{
$service = service::where('uid', $uid)->first();
$payload = [
'name' => request('name'),
'tarif' => (int) str_replace('.', '', request('tarif'))
];
$service->update($payload);
return response()->json([
'status' => 'success',
'message' => 'Data berhasil disimpan.'
], 200);
} catch (\Throwable $th) {
return response()->json([
'status' => 'error',
'message' => 'Terjadi kesalahan saat menyimpan data.',
'error' => $th->getMessage()
], 500);
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $uid)
{
try {
$service = service::where('uid', $uid)->first();
$service->update([
'is_delete' => true
]);
return response()->json([
'status' => 'success',
'message'=> 'Berhasil hapus data'
], 200);
} catch (\Throwable $th) {
return response()->json([
'status' => 'success',
'message'=> 'Berhasil hapus data'
], 500);
}
}
public function datatable(){
return service::where('is_delete', false)->get();
}
}