CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

CodeIgniter 4 delete data


ContactModel.php(Models)


<?php namespace App\Models;

use CodeIgniter\Model;

class ContactModel extends Model
{
    protected $table      = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['name', 'age', 'email'];
    protected $useTimestamps = false;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
    protected $validationRules    = [];
    protected $validationMessages = [];
    protected $skipValidation     = false;
}

contact.php(Controllers)

<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\ContactModel;
class Contact extends BaseController
{
	public function __construct(){
    }
	public function index()
	{
		$data['title']   = "Contact List";
		$model = new ContactModel();
		$data['posts'] = $model->findAll();
		return view('view',$data);
	}
	public function delete($id = null){
		$model = new ContactModel();
		$data['user'] = $model->where('id', $id)->delete();
		return redirect()->to( base_url('contact/index') );
    }
}

view.php(View)

<!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 -  posts List Example - Expertphp.in</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('contact/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>Name</th>
             <th>Age</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['name']; ?></td>
             <td><?php echo $post['age']; ?></td>
			  <td><?php echo $post['email']; ?></td>
            <td>
			<a  href="<?php echo base_url(); ?>/contact/delete/<?php echo $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>