Codeigniter 4 call model function from controller

Codeigniter 4 call model function from controller

Call model function/method from controller in CodeIgniter 4; In this tutorial, you will learn how to call model function/method from controller in codeIgniter 4 apps.

How to call model function in controller in Codeigniter 4

To call a model function within a controller in CodeIgniter 4, you need to follow these steps:

  • Step 1: Load the model on Controller
  • Step 2: Call the model function

Step 1: Load the model on Controller

Let’s say, you have a model and controller. And In your controller, you should start by loading the model you want to use. You can do this by calling the model() method within your controller’s constructor or any other method where you need to access the model. For example:

<?php
namespace App\Controllers;

use App\Models\User;

class TestController extends BaseController
{
    protected $userModel;

    public function __construct()
    {
        $this->userModel= new UserModel();
    }

    public function Index()
    {
        // Your code here
    }
}

Here’s a description of the code you provided point by point:

  1. <?php: This is the opening tag for embedding PHP code in a file. It indicates that the following code should be interpreted as PHP.
  2. namespace App\Controllers;: This line defines the namespace of the current file. It states that the file belongs to the App\Controllers namespace, which is a way to organize and group related classes and code together.
  3. use App\Models\User;: This line imports the User model class from the App\Models namespace. It allows you to refer to the User model by its short name (User) instead of the fully qualified name (App\Models\User) within this file.
  4. class TestController extends BaseController: This line declares the TestController class, which extends the BaseController class. In CodeIgniter 4, controllers are classes that handle incoming requests and perform the necessary actions. By extending the BaseController, the TestController inherits some base functionality provided by CodeIgniter.
  5. protected $userModel;: This line declares a protected property called $userModel within the TestController class. This property will hold an instance of the UserModel class.
  6. public function __construct(): This line defines the constructor method of the TestController class. The constructor is a special method that is automatically called when an object of the class is created. In this constructor, an instance of the UserModel class is created and assigned to the $userModel property.
  7. $this->userModel = new UserModel();: This line instantiates a new UserModel object and assigns it to the $userModel property of the TestController class. This allows the controller to access the functionality provided by the UserModel class.
  8. public function Index(): This line declares the Index method within the TestController class. This method represents an action that can be performed when a specific route is accessed. The Index method is typically used as the default method for a controller.
  9. // Your code here: This is a placeholder comment indicating that you can write your own code within the Index method. This is where you would add the logic for processing the request and generating the response.

Step 2: Call the model function

Once the model is loaded into your controller file, you can call its functions using the model instance created in the constructor or any other method. For example:

public function index()
{
    $result = $this->UserModel->GetAllUser();
    // Use the $result for further processing
}

The public function index() is a controller method in CodeIgniter 4 that typically serves as the entry point for a specific route. Here’s a description of above given model code:

  1. $result = $this->UserModel->GetAllUser();: This line calls the GetAllUser() function of the UserModel. It assumes that you have a model named UserModel which contains the GetAllUser() method. The purpose of this line is to retrieve all user records from the model and store the result in the $result variable.
  2. // Use the $result for further processing: This comment suggests that the $result variable obtained from the model function call is intended for further processing. You can perform various operations on the retrieved user data, such as displaying it in a view, applying data manipulation, or passing it to other methods or services.

Conclusion

That’s all; In this tutorial, you have learned how to call model function/method from controller in codeIgniter 4 apps.

Recommended CodeIgniter Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *