Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4 Tutorial
    About Lesson

    The Model is responsible for managing the data of the application. It receives user input from the Controller. It can contain functions to insert, update, and retrieve your application data.

    CodeIgniter 4 models are an essential part of its MVC (Model-View-Controller) architecture. Models handle data-related logic, including database queries, data validation, and operations like inserting, updating, and deleting records.

    Let’s explore how to use models in CodeIgniter 4 step by step.

    Model classes reside in the Models directory. They can be nested within sub-directories.

    The basic syntax for the model is:

    <?php 
    namespace App/Models;
    
    use CodeIgniter/Model;
    use CodeIgniter/Database/ConnectionInterface;
    class Model_name extends Model {
    
    	protected $db;
    	public function __construct(ConnectionInterface &$db) {
    		$this->db =& $db;
    	}
    
    }
    ?>

     

    Setting Up the Environment

    Before you start using models, ensure you have:

    • A working CodeIgniter 4 setup.
    • A database configured in the .env file.

    Example .env file configuration:

    database.default.hostname = localhost
    database.default.database = your_database
    database.default.username = your_username
    database.default.password = your_password
    database.default.DBDriver = MySQLi

     

    Creating a Model

    Models in CodeIgniter 4 are typically created in the app/Models directory. Each model is a PHP class that extends CodeIgniter/Model.

    Example: Creating a BlogModel

    1. Create a file named BlogModel.php in the app/Models directory.
    2. Add the following code:
    <?php 
    namespace App/Models;
    
    use CodeIgniter/Model;
    use CodeIgniter/Database/ConnectionInterface; class BlogModel extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; } public function add() { $data = [ 'title' => 'Title', 'description' => 'Description' ]; return $this->db ->table('blog_info') ->insert($data); } public function get_blog_list() { return $this->db ->table('blog_info') ->where(["status" => "1"]) ->get() ->getResult(); } }?>

    The file name must match the class name and the first letter of the Model class must be a capital letter.

     

    How to use Model in a Controller?

    You can use the model in controllers to interact with the database.

    use App/Models/model_name;

     

    Initialize constructor:

    public function __construct() {
    	$db = db_connect();
    	$this->model = new model_name($db);
    }

     

    If your model is located in a sub-directory, include the relative path from your model’s directory. For example, if you have a model located at /app/Models/user/Profile.php you’ll load it using the:

    use App/Models/user/Profile;
    
    public function __construct() {
    	$db = db_connect();
    	$this->profileModel = new Profile($db);
    }

     

    Once the model is loaded, you can access all the public methods using an object with the same name as your class:

    $this->profileModel>method();

     

    Here is an example of a controller, that loads a model, then serves a view:

     

    <?php 
    namespace App/Controllers;
    use App/Controllers/BaseController;
    
    use App/Models/BlogModel;
    
    class Blog extends BaseController {
        
        public function __construct() {	
    		$db = db_connect();
    		$this->blogModel = new BlogModel($db);
        }
    
    	public function index()  {
    		$data[‘list’] = $this->blogModel>get_blog_list();
    		echo view('about_us', $data);
    	}
    }
    
    ?>

    NOTE: replace (/) with a backward slash in models and controllers

     

    Methods Comparison in CodeIgniter 4 vs CodeIgniter 3

    CodeIgniter 3CodeIgniter 4
    insert();insert();
    delete();delete();
    update();update();
    row();getRow();
    result();getResult();

     

    CodeIgniter 4 models simplify database operations and integrate seamlessly into the MVC structure. With features like query builder support, data validation, and timestamps, models are powerful tools for developers. By following this tutorial, you can create and use models effectively in your projects.