Codeigniter Load Model

Codeigniter load model : $this->load->model(‘Your_model_name’); is used to load models in Codeigniter.

Codeigniter Load Model Syntax


Note if you are using subfolders in models. Example your model file is within the “application/models/users”
folder then in this case you need to load the models in this way :


For Example : load model users_model which is inside the users folder.

Codeigniter Load Model Example

Codeigniter Load Model Example

If you want to load model in library Read –

Codeigniter Load Model Example

1. Create Model :
place it in application/models folder

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

class My_first_model extends CI_Model {
 function __construct()
   public function getData(){

     $query = this->db->get('users',10);
     return $query->result_array();



2. Create Controller :
place it in application/controllers folder

Now Load model from and get data from users table.

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

class MyUsers extends CI_Controller {
   public function users(){
     //load model
     // now call model method 
     $data['users'] = $this->My_first_model->getData();
     $this->load->view('MyUsers_view', $data);



3. Create view :
place it in application/views folder

Now We are going to print user data from users table.

<title>Model Demo</title>
border:1px solid gray;
<table class="mytable">
  foreach($users as $user){
<tr><td><?php echo $user['id'];?></td><td><?php echo $user['name'];?></td><td><?php echo $user['email'];?></td><td><?php echo $user['phone'];?></td></tr>
<tr><td colspan="4">No User Found.</td></tr>

Now Access the url “http://localhost/MyUsers/users”


User Ans/Comments

Why does no one list out the model file name? Is it supposed to have _model, or _Model, or nothing as a suffix? Or all lower case?
 George Butiri
Hi George Butiri,You can take any name such as - My_Model, MyModel or mymodel. It depends up to you..
 Anil Yadav

Add Comment

📖 Read More