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