by Jairon Landa
In this tutorial, I will demonstrate and guide you on how to develop basic create, read, update, and delete operation in CodeIgniter 4 framework. The new version CodeIgniter 4, it provides you with a basic CRUD operation. To accelerate your project development.
Require in this project
GitHub Link: https://github.com/Jaironlanda/ci4-CRUD-Bootstrap4
We will create a basic Cinema project with basic CRUD operation.
Before we are going through, first we need setup
Follow this tutorial (Setup CodeIgniter 4 in XAMPP), if you still don't have CodeIgniter 4 in your local machine.
From the root of your project rename the
env file to
Uncomment the line with
CI_ENVIRONMENT on it change to
CI_ENVIRONMENT = production to
CI_ENVIRONMENT = development
Uncomment the line
app.baseURL and add a domain name.
app.baseURL = 'http://ci4site.test/'
Setup database connection variable
Note: this is default configuration for XAMPP phpmyadmin
database.default.hostname = localhost database.default.database = cinema database.default.username = root database.default.password = database.default.DBDriver = MySQLi
If everything is good, now we can start a development. Database structure for our project Cinema is
id primary key and auto increment for movie id
title movie title in string
description movie description in string
created_at date and time data is created
updated_at date and time data is updated
CREATE TABLE `cinema`.`movie` ( `id` INT(11) NOT NULL , `title` VARCHAR(355) NOT NULL , `description` TEXT NOT NULL , `created_at` DATETIME NOT NULL , `updated_at` DATETIME NOT NULL ) ENGINE = InnoDB;
app/Models/ and create new file
Copy and paste this code to
Which is your table name
Uniquely identifies the records in this table. This is useful when using find() method.
Allow CodeIgniter to save, insert, or update method any field names. If not listed will be discarded.
Determines whether current date is automatically added to all inserts and updates.
Specifies which database field should use for keeping data record create timestamp. Leave it empty to avoid update it (even useTimestamps is enabled)
Specifies which database field should use for keeping data record update timestamp. Leave it empty to avoid update it (even useTimestamps is enabled)
app/controllers/ and open
The base controller is a great place to load any helpers, models, libraries, services, etc. that you intend to use every time your project runs. (More Info)
protected $helpers = ['html', 'url', 'form'];
$this->session = \Config\Services::session();
Full code for
Controller singleton for our
Next, lets start a development.
Movie index html.
This is our index method, the purpose is to retrive all data from database.
HTML to create movie
In this method, we validate user input using CodeIgniter library to validate if user already input data in html. Else, it will show error.
HTML Edit movie detail
We retrieving our data from database, base on movie
id. If movie
id not exist. It will show pop-up error.
onClick() method to request user input before delete the movie data in database.
This method delete data in database base on primary key movie
You already learn how to use basic CRUD operation in CodeIgniter 4.
Backend and Frontend developer, a.k.a full-stack developer.Kg Bundung, Tuaran