Cara hapus data codeigniter 4 untuk pemula

Cara hapus data codeigniter 4 untuk pemula

Cara hapus data codeigniter 4 untuk pemula

Pada artikel codeigniter 4 sebelumnya telah kita lewati sebelum saya akan melanjutkan tutorial Cara hapus data codeigniter 4 untuk pemula ini. Sebaiknya kalian mengikuti tutorial sebelumnya agar semakin mudah memahami tutorial sebelumnya d bawah ini dan sebenarnya ini adalah source code lengkap dari crud sederhana codeigniter 4 dengan mysql lengkap bersama struktur databasenya

  1. Cara install composer
  2. Cara install codeigniter 4 dengan composer
  3. Cara insert data dengan codeigniter dan mysql untuk pemula
  4. Cara menampilkan data pada codeigniter 4 untuk pemula
  5. Cara update data codeigniter 4 untuk pemula

Struktur table crud codeigniter 4

CREATE TABLE `users` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`username` varchar(50) NOT NULL,

`password` varchar(50) NOT NULL,

`email` varchar(50) NOT NULL,

`created_at` timestamp(6) NULL DEFAULT current_timestamp(6),

`updated_at` timestamp(6) NULL DEFAULT NULL ON UPDATE current_timestamp(6),

`deleted_at` timestamp(6) NULL DEFAULT NULL ON UPDATE current_timestamp(6),

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

 

Nah setelah mengikuti tutorial tersebut mari kita lanjutkan ke tutorial Cara hapus data codeigniter 4 untuk pemula ini.

Kode lengkap controller crud

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

use App\Models\CrudModel;

class Crud extends BaseController

{

public function __construct(){

}

public function index()

{

$data['title'] = "Form Tambah";

$model = new CrudModel();

$data['posts'] = $model->findAll();

return view('tampil',$data);

}

public function create()

{

return view('tambah');

}




public function save(){

$data = $this->request->getVar();

$validation = \Config\Services::validation();

$validation->setRules([

'username' => 'required|string',

'password' => 'required|string',

'email' => 'required|valid_email'

]);

$res = $validation->withRequest($this->request)->run();

if(!$res){




$data['title'] = "Validasi Input";

echo view('tambah',$data, [

'validation' => $validation

]);

}

else{

$crudModel = new \App\Models\CrudModel();

$session = \Config\Services::session();

try{

$crudModel->insert($data);

$session->setFlashdata('msg', 'Record Inserted successfully');

}

catch(\Exception $e){

$session->setFlashdata('msg', 'Something went wrong');

}

$data['title'] = "Form Tambah";

echo view('tambah',$data);

}

}

public function view_edit($id = null)

{

$model = new CrudModel();

$data['user'] = $model->where('id', $id)->first();

return view('update', $data);

}

public function update($id = null){

$data = $this->request->getVar();

$id = $this->request->getVar('id');

$validation = \Config\Services::validation();

$validation->setRules([

'username' => 'required|string',

'password' => 'required|string',

'email' => 'required|valid_email'

]);

$res = $validation->withRequest($this->request)

->run();

if(!$res){




$data['title'] = "Crud Codeigniter";

echo view('update',$data, [

'validation' => $validation

]);

}

else{

//$CrudModel = new \App\Models\CrudModel();

$session = \Config\Services::session();

try{

$model = new CrudModel();

$user = $model->update($id,$data);

$session->setFlashdata('msg', 'Record Updated successfully');

}

catch(\Exception $e){

$session->setFlashdata('msg', 'Something went wrong');

}

$data['title'] = "Crud Codeigniter";

return redirect()->to( base_url('index.php/crud) );

}

}

public function delete($id = null){

$model = new CrudModel();

$data['user'] = $model->where('id', $id)->delete();

return redirect()->to( base_url('/index.php/crud) );

}

}

 

Buat View Tampilan

Buat view untuk tampilan di folder namaproject/app/views/tampil.php yang kodenya salin dari bawah ini

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>Codeigniter 4 CRUD Tutorial - LKP Naura</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">

</head>

<body>

<div class="container mt-6">

<a href="<?php echo base_url('index.php/crud/create') ?>" class="btn btn-sm btn-success">Create</a>




<div class="row mt-3">

<table class="table table-bordered" id="posts">

<thead>

<tr>

<th>Id</th>

<th>Username</th>

<th>Password</th>

<th>Email</th>

<th>Action</th>




</tr>

</thead>

<tbody>




<?php if($posts): ?>

<?php foreach($posts as $post): ?>

<tr>

<td><?php echo $post['id']; ?></td>

<td><?php echo $post['username']; ?></td>

<td><?php echo $post['password']; ?></td>

<td><?php echo $post['email']; ?></td>

<td>

<a href="<?php echo base_url(); ?>/index.php/crud/view_edit/<?=$post['id']?>">edit</a>

<a href="<?php echo base_url(); ?>/index.php/crud/delete/<?=$post['id']?>">Delete</a>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js" type="text/javascript"></script>

<script>

$(document).ready( function () {

$('#posts').DataTable();

} );

</script>

</body>

</html>

 

Buat form Update

Lalu buat form tambah untuk menambah data, di folder namaproject/app/views/tambah.php, yang codenya seperti di bawah ini

<!DOCTYPE html>

<html lang="id">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Crud Codeigniter 4 <?=$title?></title>

<!-- Bootstrap CSS -->

<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="col-lg-4">




<div class="panel panel-primary">

<div class="panel-heading">

<h3 class="panel-title">Form Tambah</h3>

</div>

<div class="panel-body">

<?= \Config\Services::validation()->listErrors(); ?>

<?= \Config\Services::session()->getFlashdata('msg'); ?>

<form method="post" action="<?= base_url() ?>/index.php/crud/save">

<div class="form-group">

<label for="name">Name:</label>

<input type="text" name="username" class="form-control" id="username">

</div>

<div class="form-group">

<label for="age">password:</label>

<input type="password" name="password" class="form-control" id=" password">

</div>

<div class="form-group">

<label for="email">Email:</label>

<input type="email" name="email" class="form-control" id="email">

</div>

<button type="submit" class="btn btn-primary">Simpan</button>

</form>

<a href="<?php echo base_url('index.php/crud') ?>" class="btn btn-sm btn-success">Kembali</a>

</div>

</div>

</div>

<!-- jQuery -->

<script src="//code.jquery.com/jquery.js"></script>

<!-- Bootstrap JavaScript -->

<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

</body>

</html>

 

Buat Tampilan Form Update

Lalu buat form update untuk mengubah data, di folder namaproject/app/views/update.php, yang codenya seperti di bawah ini

<!DOCTYPE html>

<html lang="id">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Crud Codeigniter 4 <?=$title?></title>

<!-- Bootstrap CSS -->

<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="col-lg-4">




<div class="panel panel-primary">

<div class="panel-heading">

<h3 class="panel-title">Form Tambah</h3>

</div>

<div class="panel-body">

<?= \Config\Services::validation()->listErrors(); ?>

<?= \Config\Services::session()->getFlashdata('msg'); ?>

<form method="post" action="<?= base_url() ?>/index.php/crud/update">

<input type="hidden" name="id" id="id" value="<?php echo $user['id'];?>">

<div class="form-group">

<label for="name">Name:</label>

<input type="text" name="username" value="<?php echo $user['username']; ?>" class="form-control" id="username">

</div>

<div class="form-group">

<label for="password">password:</label>

<input type="text" name="password" value="<?php echo $user['password']; ?>" class="form-control" id=" password">

</div>

<div class="form-group">

<label for="email">Email:</label>

<input type="email" name="email" value="<?php echo $user['email']; ?>" class="form-control" id="email">

</div>

<button type="submit" class="btn btn-primary">Simpan</button>

</form>

<a href="<?php echo base_url('index.php/crud') ?>" class="btn btn-sm btn-success">Kembali</a>

</div>

</div>

</div>

<!-- jQuery -->

<script src="//code.jquery.com/jquery.js"></script>

<!-- Bootstrap JavaScript -->

<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

</body>

</html>

Nah demikianlah lengkap sudah tutorial crud sederhana dengan codeigniter 4 semoga bermanfaat. jangan lupa tinggalkan komentar jika ada yang belum di pahami

 

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us