How to call model function from another model in Codeigniter?

By Hardik Savani | November 16, 2018 | Category : PHP Codeigniter

We may sometime require to call model method from another model in codeigniter app. i will show you how to call model function to another model, same model and controller file.

we always use model for database logic but i require to use for some classes like as helper. but i have more then one class as group wise and i require to call some class function to another class.

So finally i found the way to call model function to another model using "$this->load->model('Model Class Name')". I also want to give you small and quick example to call model function to another model or controller file.

In this example, i am going to create two model with Model_1 and Model_2, then i use Model_1 function to Model_2 model class. After that i created one BaseController file and class Model_2 method and print it. So let's see example:

Create Model_1 Model File:

first of all, we will create "Model_1" Model class and declare two method as fun1() and fun2(). So let's create.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Model_1 extends CI_Model {

public function fun1(){

return "Model_1 fun1";


public function fun2(){

return "Model_1 fun2";



Create Model_2 Model File:

next, we will create "Model_2" Model class and declare one method as fun1() and use Model_1 class function on it. So let's create.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Model_2 extends CI_Model {

public function __construct() {


/* Load Models - Model_1 */



public function fun1(){

/* Access Two model methods and assing response to Array */

$response[] = $this->Model_1->fun1();

$response[] = $this->Model_1->fun2();

$response[] = "Model_2 fun1";

return $response;



Create Route:

now, we will create one dummy route for testing so we can test it on our browser. So let's add route like as bellow:


$route['base-call'] = "BaseController";

Create BaseController:

at last, we will create one controller "BaseController" with index method. So let's add controller file code like as bellow:



defined('BASEPATH') OR exit('No direct script access allowed');

class BaseController extends CI_Controller {

public function __construct(){




public function index(){

$response1 = $this->Model_2->fun1();

echo "<pre>";


echo "</pre>";



Now you can simply run this example and check output.

output will be like as bellow:



[0] => Model_1 fun1

[1] => Model_1 fun2

[2] => Model_2 fun1


I hope it can help you....

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: