0

I am trying to fetch users by creating model and controller, I am just at initial stage of learning codeignitor, can anyone help?

controllers/Users.php:

<?php

class Users extends CI_Controller {
    
    public function show(){
        
        $this->load->model('user_model');
        $result = $this->user_model->get_users();
        foreach($result as $object){
            echo $object->id;
        }
    }
}

?>

models/user_model.php:

<?php

class User_model extends CI_Model {
    
    public function get_users(){
    $this->db->get('users');
    }
}

?>

Error:

An uncaught Exception was encountered
Type: RuntimeException

Message: Unable to locate the model you have specified: User_model

Filename: /home/sanadpjz/public_html/ci/system/core/Loader.php

Line Number: 314

Backtrace:

File: /home/sanadpjz/public_html/ci/application/controllers/Users.php
Line: 7
Function: model

File: /home/sanadpjz/public_html/ci/index.php
Line: 292
Function: require_once

URL vising:

example.com/ci/index.php/users/show

Database configured in config/config.php Database table 'users' exists and has 1 record.

qazidev
  • 17
  • 5

1 Answers1

0

It is clear the loader is unable to find the model file and that is because you are naming the file wrongly. The file name should start with a capital letter as the class name, mentioned in CI3 documentation.

To fix your issue change your model file from

models/user_model.php

to

models/User_model.php.

mail2bapi
  • 1,547
  • 1
  • 12
  • 18