<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Ppdb;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class PpdbController extends Controller
{
    public function index(): View
    {
        //get posts
        $posts = Ppdb::latest()->paginate(20);

        //render view with posts
        return view('datappdb', compact('posts'));
    }
    public function create(): View
    {
        return view('tambahdata');
    }
    public function store(Request $request): RedirectResponse
    { 
        //create post
        Ppdb::create([
            'jurusan'       => $request->jurusan,
            'nisn'          => $request->nisn,
            'nama'          => $request->nama,
            'alamat'        => $request->alamat,
            'tempatlahir'   => $request->tempatlahir,
        ]);

        //redirect to index
        return redirect()->route('data.index');
    }
    public function destroy($id): RedirectResponse
    {
        //get post by ID
        $post = Ppdb::findOrFail($id);

        //delete post
        $post->delete();

        //redirect to index
        return redirect()->route('data.index');
    }
}
