CodeIgniter 4 - Basic CRUD Operation Part 2 (Update and Delete)

Posted by Ridwan Fadilah on Jun 28, 2020 - 05:29 pm

Basic CRUD Operation Part 2 (Update and Delete)

Learn how to make Basic CRUD Operation in CodeIgniter 4 PHP Framework (Part 2 - Update and Delete)

CRUD are four basic in programming used for data manipulation. In part 1, I already show how to make a 'Create' and 'Read' operations. The previous tutorial also contains about how to make the basic Form Validation Rules.

Now, I'll continue the tutorial about CRUD operation in CodeIgniter 4. In this tutorial, you'll learn about how to make the basics CRUD operation 'Update' and 'Delete' by using CodeIgniter 4 Framework.

You can see the first tutorial about CodeIgniter 4 - Basic CRUD Operation and Form Validation before you start to learn this part if you didn't learn about the 'Create, Read, and Form Validation Rules' yet.

Model Configuration

You should know, In this tutorial, I applied a CodeIgniter 4 built-in CRUD methods. So, be sure you have added some configuration like the example below:

<?php

namespace App\Models;

use CodeIgniter\Model;

class YourModel extends Model
{
// your table
protected $table = 'your_table';
// primary key
protected $primaryKey = 'id';
// table fields
protected $allowedFields = ['name', 'email', 'address'];
}

The $table defines and specifies the primary database table that will work with this model. You can also use more than one table for your queries. This example is only applied for the built-in CRUD methods.

The $primaryKey is the name of the column that used as uniquely identifies in this table.

The $allowedFields specify the fields that allowed to use. This array should be the same as the field names on your database table.

Preparing The Form Edit

Prepare the page of the input form. Here, I already have the 'read' method. I'll use that to get the data first and place them into the input-form for later to update.

Didn't you learn it yet? Read the previous tutorial about 'CodeIgniter 4 - Basic CRUD and Form Validation Example (Part 1)' to know how to make the 'read' operation.

Example:

Controller

public function edit($id)
{
//load model
$model = new YourModel();

$data['forEdit'] = $model->getForEdit($id);

$data['pageTitle'] = 'Edit Page';

// load view
return view('form-folder/form-edit', $data);
}
Controller Edit Function/Get For Edit

Model

public function getForEdit($id)
{
return $this->find($id);
}

HTML Form

<?= form_open('form/update'); ?>

<form action="" method="post">
<div class="form-group">
<label for="id"></label>
<input type="hidden" class="form-control" name="id" value="<?= $forEdit['id']; ?>">
</div>

<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="<?= $forEdit['name']; ?>">
</div>

<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" value="<?= $forEdit['email']; ?>">
</div>

<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" name="address" value="<?= $forEdit['address']; ?>">
</div>

<button type="button" class="btn btn-secondary" onclick="window.location.href='<?= base_url('home'); ?>'">Back</button>
<button type="submit" class="btn btn-primary">Save Change</button>
</form>

The code above will display by the browser like this:

Form Edit

The form is auto-fill by the saved data.

Link to go to the edit page:

Go to Edit

Modify Data (Update)

The update method typically should have parameters. Parameters consist of two parameters. The first parameter is the $primaryKey of the record to update. The second parameter is an associative array of data. The array elements should match the name of the $allowedFields column in the $table set like the example above.

Example:

Form Controller

public function update()
{
$model = new YourModel();

$model->updateData();

return $this->response->redirect(site_url('home'));
}

Model

public function updateData()
{
$this->update($_POST['id'], $_POST);
}

Now, the data record can be modified.

Update Result:

Update Result

You can also use the Form Validation for this operation. You can learn a little example of CodeIgniter Form Validation at the previous tutorial.

Delete Data

The 'delete' is the most simple than another function of the 'CRUD' operations. CodeIgniter has a soft delete in its built-in 'CRUD' operation. However, I'll not show that.

The 'delete' function also needs a parameter that used as a unique identifier like the $id, $slug, etc.

Here's the example:

Controller

public function delete($id)
{
$model = new YourModel();

$model->delete($id);
return $this->response->redirect(site_url('home'));
}

Okay, that's the basics example of 'update' and 'delete' operation use in CodeIgniter 4. Find another CodeIgniter tutorial on the related tutorial below. You can see the video of this tutorial on our YouTube Channel. You can also find the full source code of these examples on our GitHub.



Thanks for reading this tutorial.


Related Articles