Membuat CRUD menggunakan CodeIgniter
Untuk melakukan CRUD di CodeIgniter 4 terdapat beberapa langkah yaitu sebagai berikut.
- Instalasi CodeIgniter 4
- Menyiapkan Database
- Membuat Model
- Membuat View
- Mengatur Controller
- Mengatur Routes
Langkah pertama adalah melakukan instalasi code igniter yang pernah dibahas sebelumnya disini. Oleh karena itu kita lanjut ke bahasan membuat database. Untuk databasenya sendiri saya menggunakan MySql bawaan dari XAMPP. Dalam database ini, kita perlu membuat table sebagai berikut.
Jika sudah, kita lakukan konfigurasi pada database yang digunakan pada App/Config/Database.php sebagai berikut.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public $default = [ | |
'DSN' => '', | |
'hostname' => 'localhost', | |
'username' => 'root', | |
'password' => '', | |
'database' => 'db_mahasiswa', | |
'DBDriver' => 'MySQLi', | |
'DBPrefix' => '', | |
'pConnect' => false, | |
'DBDebug' => (ENVIRONMENT !== 'production'), | |
'charset' => 'utf8', | |
'DBCollat' => 'utf8_general_ci', | |
'swapPre' => '', | |
'encrypt' => false, | |
'compress' => false, | |
'strictOn' => false, | |
'failover' => [], | |
'port' => 3306, | |
]; |
Kemudian kita perlu membuat model MahasiswaModel.php, isikan model sebagai berikut.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use CodeIgniter\Model; | |
class MahasiswaModel extends Model | |
{ | |
protected $table = 'mahasiswa'; | |
protected $primaryKey = 'id'; | |
protected $allowedFields = ['nrp', 'nama', 'jurusan', 'alamat', 'nohp']; | |
public function data_mhs($id_mhs) | |
{ | |
return $this->find($id_mhs); | |
} | |
public function update_data($data, $id_mhs) | |
{ | |
$query = $this->db->table($this->table)->update($data, array('id' => $id_mhs)); | |
return $query; | |
} | |
public function delete_data($id_mhs) | |
{ | |
$query = $this->db->table($this->table)->delete(array('id' => $id_mhs)); | |
return $query; | |
} | |
} |
Langkah Selanjutnya kita buat template untuk view kita dalam folder template/template.php sebagai berikut.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<!-- Required meta tags --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- Bootstrap CSS --> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> | |
<title>CRUD Mahasiswa!</title> | |
</head> | |
<body> | |
<div class="container"> | |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> | |
<a class="navbar-brand" href="#">CRUD CodeIgniter4</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse" id="navbarSupportedContent"> | |
<ul class="navbar-nav mr-auto"> | |
<li class="nav-item active"> | |
<a class="nav-link" href="/">Home<span class="sr-only"></span></a> | |
</li> | |
</div> | |
</nav> | |
</div> | |
<?= $this->renderSection('content') ?> | |
<footer class="footer"> | |
<div class="container"> | |
<span class="text-muted">Muhammad Ilham Bayhaqi 2021</span> | |
</div> | |
</footer> | |
</body> | |
</html> |
Karena template untuk view telah dibuat, kita tinggal membuat viewnya. Untuk view nya sebagai berikut.
- mahasiswa/data_mahasiswa.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= $this->extend('template/template') ?> | |
<?= $this->section('content') ?> | |
<div class="container"> | |
<div class="card mt-3"> | |
<div class="card-header"> | |
<b><?= $title ?></b> | |
</div> | |
<div class="card-body"> | |
<a href="mahasiswa/tambah" class="btn btn-info">Tambah </a> | |
<br><br> | |
<table class="table table-bordered"> | |
<tr> | |
<th>No</th> | |
<th>NRP</th> | |
<th>Nama</th> | |
<th>Jurusan</th> | |
<th>Alamat</th> | |
<th>No. HP</th> | |
<th>Aksi</th> | |
</tr> | |
<?php | |
$no = 1; | |
foreach ($mahasiswa as $key) : ?> | |
<tr> | |
<td><?php echo $no++; ?></td> | |
<td><?php echo $key['nrp']; ?></td> | |
<td><?php echo $key['nama']; ?></td> | |
<td><?php echo $key['jurusan']; ?></td> | |
<td><?php echo $key['alamat']; ?></td> | |
<td><?php echo $key['nohp']; ?></td> | |
<td> | |
<a href="mahasiswa/edit/<?php echo $key['id']; ?>">Edit</a> | |
<a href="mahasiswa/delete/<?php echo $key['id']; ?>">Hapus</a> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
</div> | |
</div> | |
</div> | |
<?= $this->endSection() ?> |
- mahasiswa/data_tambah.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $this->extend('template/template'); ?> | |
<?php $this->section('content'); ?> | |
<div class="container"> | |
<div class="card mt-3"> | |
<div class="card-header"> | |
<b><?= $title ?></b> | |
</div> | |
<div class="card-body"> | |
<form action="<?php echo base_url('mahasiswa/simpan') ?>" method="post"> | |
<div class="form-group row"> | |
<label for="nrp" class="col-sm-2 label">NRP</label> | |
<div class="col-sm-4"> | |
<input type="text" name="nrp" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nama" class="col-sm-2 label">Nama</label> | |
<div class="col-sm-4"> | |
<input type="text" name="nama" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="jurusan" class="col-sm-2 label">Jurusan</label> | |
<div class="col-sm-4"> | |
<input type="text" name="jurusan" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="alamat" class="col-sm-2 label">Alamat</label> | |
<div class="col-sm-5"> | |
<input type="text" name="alamat" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nohp" class="col-sm-2 label">No Hp</label> | |
<div class="col-sm-5"> | |
<input type="text" name="nohp" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<div class="col-sm-4"> | |
<input type="submit" value="submit" class="btn btn-info"> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
<?php $this->endSection(); ?> |
- mahasiswa/data_edit.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $this->extend('template/template'); ?> | |
<?php $this->section('content'); ?> | |
<div class="container"> | |
<div class="card mt-3"> | |
<div class="card-header"> | |
<b><?= $title ?></b> | |
</div> | |
<div class="card-body"> | |
<form action="<?php echo base_url('mahasiswa/update') ?>" method="post"> | |
<div class="form-group row"> | |
<input type="hidden" name="id" value="<?= $mahasiswa['id']; ?>"> | |
<label for="nama" class="col-sm-2 label">NRP</label> | |
<div class="col-sm-4"> | |
<input type="text" name="nrp" class="form-control" value="<?= $mahasiswa['nrp']; ?>"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nama" class="col-sm-2 label">Nama</label> | |
<div class="col-sm-4"> | |
<input type="text" name="nama" class="form-control" value="<?= $mahasiswa['nama']; ?>"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nama" class="col-sm-2 label">Jurusan</label> | |
<div class="col-sm-4"> | |
<input type="text" name="jurusan" class="form-control" value="<?= $mahasiswa['jurusan']; ?>"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nama" class="col-sm-2 label">Alamat</label> | |
<div class="col-sm-5"> | |
<input type="text" name="alamat" class="form-control" value="<?= $mahasiswa['alamat']; ?>"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="nama" class="col-sm-2 label">No HP</label> | |
<div class="col-sm-5"> | |
<input type="text" name="nohp" class="form-control" value="<?= $mahasiswa['nohp']; ?>"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<div class="col-sm-4"> | |
<input type="submit" value="submit" class="btn btn-info"> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
<?php $this->endSection(); ?> |
Untuk logic nya, kita bisa buat sebuah controller bernama Mahasiswa.php dan masukkan code berikut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controllers; | |
use App\Models\MahasiswaModel; | |
class Mahasiswa extends BaseController | |
{ | |
protected $mahasiswamodel; | |
public function __construct() | |
{ | |
$this->mahasiswamodel = new MahasiswaModel(); | |
} | |
public function index() | |
{ | |
$mahasiswa = $this->mahasiswamodel->findAll(); | |
$data = [ | |
'title' => 'Data Mahasiswa', | |
'mahasiswa' => $mahasiswa | |
]; | |
echo view('mahasiswa/data_mahasiswa', $data); | |
} | |
public function tambah() | |
{ | |
$data = [ | |
'title' => 'Tambah Data Mahasiswa', | |
]; | |
echo view('mahasiswa/data_tambah', $data); | |
} | |
public function simpan() | |
{ | |
$this->mahasiswamodel->save([ | |
'nrp' => $this->request->getVar('nrp'), | |
'nama' => $this->request->getVar('nama'), | |
'jurusan' => $this->request->getVar('jurusan'), | |
'alamat' => $this->request->getVar('alamat'), | |
'nohp' => $this->request->getVar('nohp') | |
]); | |
return redirect()->to('/mahasiswa'); | |
} | |
public function edit($id_mhs) | |
{ | |
$mahasiswa = $this->mahasiswamodel->data_mhs($id_mhs); | |
$data = [ | |
'title' => 'Edit Data Mahasiswa', | |
'mahasiswa' => $mahasiswa | |
]; | |
echo view('mahasiswa/data_edit', $data); | |
} | |
public function update() | |
{ | |
$id_mhs = $this->request->getVar('id'); | |
$data = [ | |
'nrp' => $this->request->getVar('nrp'), | |
'nama' => $this->request->getVar('nama'), | |
'jurusan' => $this->request->getVar('jurusan'), | |
'alamat' => $this->request->getVar('alamat'), | |
'nohp' => $this->request->getVar('nohp') | |
]; | |
$this->mahasiswamodel->update_data($data, $id_mhs); | |
return redirect()->to('/mahasiswa'); | |
} | |
public function delete($id_mhs) | |
{ | |
$this->mahasiswamodel->delete_data($id_mhs); | |
return redirect()->to('/mahasiswa'); | |
} | |
} |
Terakhir kita lakukan assign controller Mahasiswa.php pada Config/Routes.php
Daaan, jadi deh CRUD Mahasiswa sederhana. Untuk hasilnya kira kira sebagai berikut.
Komentar
Posting Komentar