How will you add or load a model in Codeigniter?

devquora
devquora

Posted On: Feb 22, 2018

 

In Codeigniter, models will typically be loaded and called from within your controller methods. To load a model you will use the following method:

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

If your model is located in a sub-directory, include the relative path from your model’s directory. For example, if you have a model located at application/models/blog/Posts.php you’ll load it using:

$this->load->model('blog/Posts');

Once your Model loaded, you will access its methods using an object with the same name as your controller:

Example

class Blog_controller extends CI_Controller {
        public function blog()
        {
                $this->load->model('blog');

                $data['query'] = $this->blog->get_last_ten_entries();

                $this->load->view('blog', $data);
        }
}

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    CodeIgniter Interview Questions

    How to check the version of CodeIgniter framework?

    How to check the version of CodeIgniter framework?..