Create, Update, Delete - Laravel #2

Create Data

  1. Tambahkan route untuk create and store di routes/web.php
Route::get('/pasien', [PasienController::class, 'index'])->name('pasien.index');
Route::get('/pasien/tambah_pasien', [PasienController::class, 'create'])->name('pasien.create');
Route::post('/pasien/pasien', [PasienController::class, 'store'])->name('pasien.store');

2. Tambahkan Method create() dan store() di Controller

public function create()
   {
    $kelurahan = Kelurahan::all(); // ambil data kelurahan
    return view("pasien.tambah_pasien", compact("kelurahan"));
   }

public function store(Request $request)
{
    $request->validate([
        'kode' => 'required|max:10',
        'name' => 'required',
        'tmp_lahir' => 'required',
        'tgl_lahir' => 'required|date',
        'gender' => 'required',
        'email' => 'required|email',
        'alamat' => 'required',
        'kelurahan_id' => 'required|exists:kelurahan,id'
    ]);

    Pasien::create($request->all());
    return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil ditambahkan');
}
  1. Update Model Pasien.php
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Pasien extends Model
{
    use HasFactory;
    protected $table = "pasien";

    protected $fillable = [
    'kode', 'name', 'tmp_lahir', 'tgl_lahir', 'gender', 'email', 'alamat', 'kelurahan_id'
    ];

}

4. Buat Form Tambah: resources/views/pasien/tambah_pasien.blade.php

<form action="{{ route('pasien.store') }}" method="POST">
    @csrf
    <input type="text" name="kode" placeholder="Kode"><br>
    <input type="text" name="name" placeholder="Nama"><br>
    <input type="text" name="tmp_lahir" placeholder="Tempat Lahir"><br>
    <input type="date" name="tgl_lahir"><br>
    <select name="gender">
        <option value="L">Laki-laki</option>
        <option value="P">Perempuan</option>
    </select><br>
    <input type="email" name="email" placeholder="Email"><br>
    <textarea name="alamat" placeholder="Alamat"></textarea><br>
    <select name="kelurahan_id">
    @foreach ($kelurahan as $k)
        <option value="{{ $k->id }}">{{ $k->name }}</option>
    @endforeach
</select>
    <button type="submit">Simpan</button>
</form>

5. Tambahkan Tombol Tambah di pasien.blade.php

<a href="{{ route('pasien.create') }}">โž• Tambah Pasien</a>

Update Data

1, Tambahkan route untuk edit atau update data

// route untuk edit data 
Route::get('/pasien/{id}/edit', [PasienController::class, 'edit'])->name('pasien.edit');
Route::put('/pasien/{id}', [PasienController::class, 'update'])->name('pasien.update');

2. Tambahkan Method edit() dan update() di Controller

public function edit($id)
{
    $pasien = Pasien::findOrFail($id);
    $kelurahan = Kelurahan::all();
    return view('pasien.edit_pasien', compact('pasien', 'kelurahan'));
}

public function update(Request $request, $id)
{
    $request->validate([
        'kode' => 'required|max:10',
        'name' => 'required',
        'tmp_lahir' => 'required',
        'tgl_lahir' => 'required|date',
        'gender' => 'required',
        'email' => 'required|email',
        'alamat' => 'required',
        'kelurahan_id' => 'required|exists:kelurahan,id'
    ]);

    $pasien = Pasien::findOrFail($id);
    $pasien->update($request->all());

    return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil diupdate');
}

3. Buat Form Edit: resources/views/pasien/edit_pasien.blade.php

<form action="{{ route('pasien.update', $pasien->id) }}" method="POST">
    @csrf
    @method('PUT')
    <input type="text" name="kode" value="{{ $pasien->kode }}"><br>
    <input type="text" name="name" value="{{ $pasien->name }}"><br>
    <input type="text" name="tmp_lahir" value="{{ $pasien->tmp_lahir }}"><br>
    <input type="date" name="tgl_lahir" value="{{ $pasien->tgl_lahir }}"><br>
    <select name="gender">
        <option value="L" {{ $pasien->gender == 'L' ? 'selected' : '' }}>Laki-laki</option>
        <option value="P" {{ $pasien->gender == 'P' ? 'selected' : '' }}>Perempuan</option>
    </select><br>
    <input type="email" name="email" value="{{ $pasien->email }}"><br>
    <textarea name="alamat">{{ $pasien->alamat }}</textarea><br>
    <select name="kelurahan_id">
        @foreach ($kelurahan as $k)
            <option value="{{ $k->id }}" {{ $pasien->kelurahan_id == $k->id ? 'selected' : '' }}>
                {{ $k->name }}
            </option>
        @endforeach
    </select><br>
    <button type="submit">Update</button>
</form>

4. Tambahkan Tombol Edit di Tabel tampil_pasien.blade.php

<td>
    <a href="{{ route('pasien.edit', $pasien->id) }}">โœ๏ธ Edit</a>
</td>

DELETE

1. Tambahkan Route


Route::delete('/pasien/{id}', [PasienController::class, 'destroy'])->name('pasien.destroy');

2. Tambahkan Method destroy() di Controller

public function destroy($id)
{
    $pasien = Pasien::findOrFail($id);
    $pasien->delete();
    return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil dihapus');
}

3. Tambahkan Tombol Hapus di tampil_pasien.blade.php

<td>
        <form action="{{ route('pasien.destroy', $pasien->id) }}" method="POST" style="display: inline;" onsubmit="return confirm('Yakin ingin hapus?')">
        @csrf
        @method('DELETE')
        <button type="submit" style="background: none; border: none; color: red; cursor: pointer;">๐Ÿ—‘๏ธ Hapus</button>
    </form>
</td>
0
Subscribe to my newsletter

Read articles from Astrid Annasya Putri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Astrid Annasya Putri
Astrid Annasya Putri