Posted by Ridwan Fadilah on Jun 28, 2020 - 05:29 pm
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.
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:
$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.
$primaryKey is the name of the column that used as uniquely identifies in this table.
$allowedFields specify the fields that allowed to use. This array should be the same as the field names on your database table.
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.
The code above will display by the browser like this:
The form is auto-fill by the saved data.
Link to go to the edit page:
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.
Now, the data record can be modified.
You can also use the Form Validation for this operation. You can learn a little example of CodeIgniter Form Validation at the previous tutorial.
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
Here's the example:
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.