In this tutorial, We will learn how to call a model method from another model in the Codeigniter app. I will teach you how to call a model function to another model, the same model, and the controller file.

Follow some steps:

#### Step 1:

Create Model_1 Model File application/models/Model_1.php

``````<?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";
}

}``````

#### Step 1:

Create Model_2 Model File application/models/Model_2.php

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

Class Model_2 extends CI_Model {

public function __construct() {
parent::__construct();

/* Load Models - Model_1 */
\$this->load->model('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;
}

}``````

#### Step 3:

Create Route application/config/routes.php

``````<?php

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

#### Step 4:

Create BaseController application/controller/BaseController.php

``````<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class BaseController extends CI_Controller {

public function __construct(){
parent::__construct();
\$this->load->model('Model_2');
}

public function index(){

\$response1 = \$this->Model_2->fun1();
echo "<pre>";
print_r(\$response1);
echo "</pre>";

}

}``````

Output:

 Array (      => Model_1 fun1      => Model_1 fun2      => Model_2 fun1 )

Thanks, I hope it will work for you.........
{"ft_lang_label":"__label__en","ft_lang_prob":0.5916171,"math_prob":0.6701545,"size":1611,"snap":"2021-21-2021-25","text_gpt3_token_len":411,"char_repetition_ratio":0.1468575,"word_repetition_ratio":0.09090909,"special_character_ratio":0.31843576,"punctuation_ratio":0.1977612,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96875477,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-22T16:35:57Z\",\"WARC-Record-ID\":\"<urn:uuid:62b3caf7-32b9-45db-a8d9-1bfce2f70965>\",\"Content-Length\":\"35428\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2de01a96-18af-4a44-b5ec-3a0f58a9b423>\",\"WARC-Concurrent-To\":\"<urn:uuid:1fe03e2f-67c6-4280-a5f6-0dca1e087fa8>\",\"WARC-IP-Address\":\"103.148.157.253\",\"WARC-Target-URI\":\"https://rathorji.in/p/how_to_call_a_model_function_from_another_model_in_codeigniter\",\"WARC-Payload-Digest\":\"sha1:TCLLWXXWKAC2H7QAIFCCJQJCFWEPVBU7\",\"WARC-Block-Digest\":\"sha1:I6KMT6A5EXIDR6TONSO2RVC3OXLCBCOM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488519183.85_warc_CC-MAIN-20210622155328-20210622185328-00305.warc.gz\"}"}