MIF_E31211936/app/Http/Controllers/SymptomController.php

138 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\symptom;
use Illuminate\Http\Request;
class SymptomController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$symptom = symptom::all();
return view('admin.symptom.symptom', compact('symptom'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
$messages = [
'kodegejala2.required' => 'Kode gejala harus diisi.',
'kodegejala2.string' => 'Kode gejala harus berupa string.',
'kodegejala2.max' => 'Kode gejala tidak boleh lebih dari 10 karakter.',
'gejala2.required' => 'Gejala harus diisi.',
'gejala2.string' => 'Gejala harus berupa string.',
];
$validatedData = $request->validate([
'kodegejala2' => 'required|string|max:10',
'gejala2' => 'required|string'
], $messages);
$existingSymptom = Symptom::where('code', $validatedData['kodegejala2'])
->orWhere('name', $validatedData['gejala2'])
->first();
if ($existingSymptom) {
return back()->withErrors(['error' => 'Gejala dengan kode atau nama yang sama sudah ada.']);
}
$symptom = new Symptom();
$symptom->code = $validatedData['kodegejala2'];
$symptom->name = $validatedData['gejala2'];
$symptom->save();
return back()->with('success', 'Gejala berhasil ditambahkan.');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$messages = [
'kodegejala.required' => 'Kode gejala harus diisi.',
'kodegejala.string' => 'Kode gejala harus berupa string.',
'kodegejala.max' => 'Kode gejala tidak boleh lebih dari 10 karakter.',
'gejala.required' => 'Gejala harus diisi.',
'gejala.string' => 'Gejala harus berupa string.',
];
$validatedData = $request->validate([
'kodegejala' => 'required|string|max:10',
'gejala' => 'required|string'
], $messages);
$symptom = Symptom::findOrFail($id);
$symptom->code = $validatedData['kodegejala'];
$symptom->name = $validatedData['gejala'];
$symptom->save();
return back()->with('success', 'Gejala berhasil diupdate.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$symptom = symptom::findOrFail($id);
$symptom->delete();
return back()->with('success', 'Berhasil menghapus.');
}
}