MIF_E31222596/website/app/Http/Controllers/CatatanKesehatanController.php

131 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\CatatanKesehatan;
use App\Models\Santri;
use App\Models\Kelas;
use Illuminate\Http\Request;
class CatatanKesehatanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$query = CatatanKesehatan::with(['santri', 'kelas'])->latest();
// filter berdasarkan search
if ($request->has('search') && $request->search != '') {
$query->whereHas('santri', function ($q) use ($request) {
$q->where('nama', 'like', '%' . $request->search . '%');
// sesuaikan 'nama' dengan kolom di tabel santris
});
}
$catatanKesehatans = $query->paginate(10)->withQueryString();
$view = auth()->user()->isGuru() ? 'guru.catatan_kesehatans.index' : 'catatan_kesehatans.index';
return view($view, compact('catatanKesehatans'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$santris = Santri::all();
$kelas = Kelas::all();
$view = auth()->user()->isGuru() ? 'guru.catatan_kesehatans.create' : 'catatan_kesehatans.create';
return view($view, compact('santris', 'kelas'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'santri_id' => 'required|exists:santris,id',
'kelas_id' => 'required|exists:kelas,id',
'keluhan' => 'required|string',
'diagnosis' => 'required|string',
'saran' => 'nullable|string',
]);
CatatanKesehatan::create($validated);
return redirect()->route('catatan_kesehatans.index')
->with('success', 'Catatan kesehatan berhasil ditambahkan.');
}
/**
* Display the specified resource.
*/
public function show(CatatanKesehatan $catatanKesehatan)
{
$catatanKesehatan->load(['santri', 'kelas']);
$view = auth()->user()->isGuru() ? 'guru.catatan_kesehatans.show' : 'catatan_kesehatans.show';
return view($view, compact('catatanKesehatan'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(CatatanKesehatan $catatanKesehatan)
{
$santris = Santri::all();
$kelas = Kelas::all();
$view = auth()->user()->isGuru() ? 'guru.catatan_kesehatans.edit' : 'catatan_kesehatans.edit';
return view($view, compact('catatanKesehatan', 'santris', 'kelas'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, CatatanKesehatan $catatanKesehatan)
{
$validated = $request->validate([
'santri_id' => 'required|exists:santris,id',
'kelas_id' => 'required|exists:kelas,id',
'keluhan' => 'required|string',
'diagnosis' => 'required|string',
'saran' => 'nullable|string',
]);
$catatanKesehatan->update($validated);
return redirect()->route('catatan_kesehatans.index')->with('success', 'Catatan kesehatan berhasil diupdate.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(CatatanKesehatan $catatanKesehatan)
{
$catatanKesehatan->delete();
return redirect()->route('catatan_kesehatans.index')->with('success', 'Catatan kesehatan berhasil dihapus.');
}
}
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\CatatanKesehatan;
use Illuminate\Http\Request;
class KesehatanApiController extends Controller
{
public function index(Request $request)
{
$user = $request->user();
$catatan = CatatanKesehatan::where('user_id', $user->id)->get();
return response()->json($catatan);
}
public function notifikasi(Request $request)
{
$user = $request->user();
$notifikasi = CatatanKesehatan::where('user_id', $user->id)->whereNotNull('keluhan')->get();
return response()->json($notifikasi);
}
}