Posted by Ridwan Fadilah on Jun 26, 2020 - 02:51 am
Learn the Basic CRUD and Form Validation Examples of CodeIgniter 4. Accessing, Creating, Connecting to Database, and Configuring your Model.
CRUD (Create, Read, Update, Delete) is the most needed to make a dynamic website. CRUD are four basic in programming used for data manipulation. You can use it for inserting data, finding data, modify data, and deleting data from the database.
In this tutorial, I'll show you an example of how to make CRUD operation and Validate the data input by using the CodeIgniter 4 Framework.
The official CodeIgniter has told the users about their new Model features in this version.
CodeIgniter does provide a model class that provides a few nice features, including:
This class provides a solid base from which to build your own models, allowing you to rapidly build out your application’s model layer.
The first is creating your model. You can start to make your CRUD operation from here.
You would find the Models folder in the
app folder. Create a new file in the Models folder and named as do you want. After that, fill the blank document with this following syntax:
Important to note: The
classname is should the same as the file name.
Let's configure your model. The model has a few configurations. I'll show you how to config your model but, I just to show you a little config. You can find the full config on the CodeIgniter 4 Official user guide.
$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.
Here's the table structure:
Models are stored in the
app/Models directory. To call them in your controller just add this following syntax below the namespace line:
To make it easier to understand, I'll show you the folder structure for this example:
To make a '
create' operation of CRUD, we'll work with three files:
Prepare your form input page:
<?= form_open('your-controller/create'); ?> line is the form action, so, keep the
action attribute blank.
Be sure the value of the '
for' attribute is the same as the '
<?= set_value(); ?> is used to keep the input value when the input is not valid.
I use the
$_POST (super global variable) to make it simpler.
'new' at this line
$validation->run($_POST, 'new'), is the variable name of the validation rules.
The validation didn't work yet. You should set the form validation rules first to make it work.
echo '<div>' . $validation->listErrors() . '</div>'; is the error messages. That will display by the browser like this:
To make the '
create' function above work, you should set the form validation rules first. Configure the validation in the
Validation.php file. You can find it on the
Here's the example of the validation rules:
$new is the variable name of the validation rules, use that to call the validation rules on your controller.
Now, you can insert data into the database. The function/operation now could be used.
Finding data are simpler than insert data because you no need to use the validation. By using the built-in CRUD operation in CodeIgniter 4, you can run the 'read' function with a short code, except you specify the queries of the specific data.
I want to display the query on my home page.
The page structure:
The code above will display by browser like this:
orderBy() is used to sort the query result by name.
Okay, that's the basic example of 'create' and 'read' operation use in CodeIgniter 4. Find another example of CRUD operation 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.