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\Controllersnamespace, which is a way to organize and group related classes and code together.use App\Models\User;: This line imports theUsermodel class from theApp\Modelsnamespace. It allows you to refer to theUsermodel by its short name (User) instead of the fully qualified name (App\Models\User) within this file.class TestController extends BaseController: This line declares theTestControllerclass, which extends theBaseControllerclass. In CodeIgniter 4, controllers are classes that handle incoming requests and perform the necessary actions. By extending theBaseController, theTestControllerinherits some base functionality provided by CodeIgniter.protected $userModel;: This line declares a protected property called$userModelwithin theTestControllerclass. This property will hold an instance of theUserModelclass.public function __construct(): This line defines the constructor method of theTestControllerclass. The constructor is a special method that is automatically called when an object of the class is created. In this constructor, an instance of theUserModelclass is created and assigned to the$userModelproperty.$this->userModel = new UserModel();: This line instantiates a newUserModelobject and assigns it to the$userModelproperty of theTestControllerclass. This allows the controller to access the functionality provided by theUserModelclass.public function Index(): This line declares theIndexmethod within theTestControllerclass. This method represents an action that can be performed when a specific route is accessed. TheIndexmethod 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 theIndexmethod. 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 namedUserModelwhich contains theGetAllUser()method. The purpose of this line is to retrieve all user records from the model and store the result in the$resultvariable.// Use the $result for further processing: This comment suggests that the$resultvariable 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.