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

120 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\helper\helperController;
use App\Models\asuransi;
use Illuminate\Http\Request;
class AsuransiController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$data =[
'title' => 'Asuransi',
];
return view('asuransi.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 {
$names = request('name');
foreach ($names as $name) {
if (!empty($name)) {
$payload = [
'name' => $name,
'uid'=> (new helperController)->getUid()
];
asuransi::create($payload);
}
}
return response()->json([
'status' => 'success',
'message' => 'Berhasil! menambahkan data'
]);
} catch (\Throwable $th) {
return response()->json([
'status' => 'error',
'message' => 'gagal! menambahkan data' . $th->getMessage()
]);
}
}
/**
* Display the specified resource.
*/
public function show(asuransi $asuransi)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(asuransi $asuransi)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $uid)
{
try {
$asuransi = asuransi::where('uid', $uid)->first();
$payload = ['name' => request('name')];
$asuransi->update($payload);
return response()->json([
'status' => 'success',
'message' => 'Berhasil! edit data'
]);
} catch (\Throwable $th) {
return response()->json([
'status' => 'error',
'message' => 'gagal! edit data' . $th->getMessage()
]);
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $uid)
{
try {
asuransi::where('uid', $uid)->update([
'is_delete' => true
]);
return response()->json([
'status' => 'success',
'message' => 'Berhasil! Hapus data'
]);
} catch (\Throwable $th) {
return response()->json([
'status' => 'error',
'message' => 'gagal! hapus data' . $th->getMessage()
]);
}
}
public function datatable(){
return asuransi::where('is_delete', false)->get();
}
}