Articles → PHP CODEIGNITER → Model In Codeigniter

Model In Codeigniter






Purpose





How To Create A Model?






<?php
class mymodel extends CI_Model{
    public function DoEcho()
    {
        echo "My first model";
    }
}





How To Call A Model In Controller Class?




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

class About extends CI_Controller{
    public function Index(){
        $this->load->model('mymodel');
        $this->mymodel->DoEcho();
        $this->load->view('About');
    }
}




Picture showing the output of text displayed when models method is called from the controller
Click to Enlarge


Model Alias




$this->load->model('mymodel');




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

class About extends CI_Controller{
    public function Index(){
        $this->load->model('mymodel', 'mm');
        $this->mm->DoEcho();
        $this->load->view('About');
    }
}





Posted By  -  Karan Gupta
 
Posted On  -  Monday, July 13, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250