In Codeigniter 4, you can call a model function from a controller using the following syntax:
$this->modelName->functionName();
For example, if you have a model named User_model and a function named get_user_details(), you can call it from a controller like this:
$this->User_model->get_user_details();
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:
<?php
: This is the opening tag for embedding PHP code in a file. It indicates that the following code should be interpreted as PHP.namespace App\Controllers;
: This line defines the namespace of the current file. It states that the file belongs to theApp\Controllers
namespace, which is a way to organize and group related classes and code together.use App\Models\User;
: This line imports theUser
model class from theApp\Models
namespace. It allows you to refer to theUser
model by its short name (User
) instead of the fully qualified name (App\Models\User
) within this file.class TestController extends BaseController
: This line declares theTestController
class, which extends theBaseController
class. In CodeIgniter 4, controllers are classes that handle incoming requests and perform the necessary actions. By extending theBaseController
, theTestController
inherits some base functionality provided by CodeIgniter.protected $userModel;
: This line declares a protected property called$userModel
within theTestController
class. This property will hold an instance of theUserModel
class.public function __construct()
: This line defines the constructor method of theTestController
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 theUserModel
class is created and assigned to the$userModel
property.$this->userModel = new UserModel();
: This line instantiates a newUserModel
object and assigns it to the$userModel
property of theTestController
class. This allows the controller to access the functionality provided by theUserModel
class.public function Index()
: This line declares theIndex
method within theTestController
class. This method represents an action that can be performed when a specific route is accessed. TheIndex
method is typically used as the default method for a controller.// Your code here
: This is a placeholder comment indicating that you can write your own code within theIndex
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:
$result = $this->UserModel->GetAllUser();
: This line calls theGetAllUser()
function of theUserModel
. It assumes that you have a model namedUserModel
which contains theGetAllUser()
method. The purpose of this line is to retrieve all user records from the model and store the result in the$result
variable.// 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.