K.H.
K.H.

Reputation: 111

Codeigniter: Models not working with an error: Undefined property: CI_Loader

I'm trying out making a simple API with Codeigniter, which is supposed to do basic CRUD tasks on the database related with the form inputs from the main website. I'm mostly a frontend person and don't really have an in-depth knowledge with PHP. It's my first time using the Codeigniter. I'm starting with trying out things on the documentation website, but I ran into a problem working with models.

before actually working with databases, I tried writing a really simple model that just returns "Model_form_receipt" when called. The website gives me an Undefined property: CI_Loader error. This is really troubling because I don't think I even deviated much from the samples from the documentation; perhaps even simpler. Do you have any idea on the cause of this issue and a solution?

The model file (models/Model_form_receipt.php)

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

class Model_form_receipt extends CI_Model {
  public function test()  {
    return "Model_form_receipt";
  }
}
?>

The view file

<?php
$this->load->model('model_form_receipt');
$data = $this->model_form_receipt->test();
?>

Error Messages

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$model_form_receipt

Filename: form_receipt/base.php

Line Number: 3
An uncaught Exception was encountered

Type: Error

Message: Call to a member function test() on null

Filename: [REDACTED]

Line Number: 3

Upvotes: 0

Views: 710

Answers (1)

Fahim Sultan
Fahim Sultan

Reputation: 387

In Your Controller

**Autoload the model in __construct function.

Thus, your model will run automatically in your function**

It could help

public function __construct(){
    parent::__construct();
    $this->load->model('Model_form_receipt');
}

Upvotes: 2

Related Questions