Takaapo Page Takaapo Page - 1 year ago 125
PHP Question

Can't find model in CodeIgniter controller

I load the

model in line 8, but when I want to use
model to load
method in line 23, the error occur and say
Undefined property: Notes::$Get_notes
in line 23 !
There is something wrong in line 23 but I dont know what is that.Please help me.


class Notes extends CI_Controller

public function index()
$data['notes'] = $this->Get_notes->get_mm();
$this->load->view('show', $data);

public function insert()
$title = 'Something';
$text = 'Something else';

$data = [
'title' => $title,
'text' => $text


Answer Source

The way you've written it, your Get_notes Model is only available within your index() function.

To make your Get_notes Model available to all functions within a single Controller, load it within that Controller's constructor function...

class Notes extends CI_Controller {

    public function __construct()
        $this->load->model('get_notes'); // available to all functions within Notes 

To make your Get_notes Model available globally to all functions in any CI Controller, put it in your $autoload['model'] array within the autoload.php file located at application/config/autoload.php...

$autoload['model'] = array('get_notes'); // available to all functions in your CI application

→ Note that it's supposed to be written in all lower-case however you reference it.



If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:

parent::__construct(); // Notice there is no dollar sign

Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

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


Once loaded, you will access your model methods using an object with the same name as your class:


To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You’ll find instructions in that file corresponding to each type of item.