Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is wrong with this line?

I am new to codeigniter and i am stuck trying to figure out what is going wrong on this line

here is my controller

class Product extends CI_Controller{

    function index(){
        $this->load->model('product_model');
        $data['products'] = $this->product_model->get_all_products();
        $this->load->view('all_products', $data); 

    }
}

here is my model

class Product_model extends CI_Model {

    function get_all_products(){
        $query = $this->db->get('products');
        if($query->num_rows() > 0){
            foreach($query->result() as $row){
                $data[] = $row;
            } 
            return $data;
        }
    }
}

and here is my error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Product::$db

Filename: core/Model.php

Line Number: 50
Fatal error: Call to a member function get() on a non-object in /Users/matt/Sites/ci/application/models/product_model.php on line 9el.php on line 6

the error is on this line

$query = $this->db->get('products');

why is it failing the codeigniter documentation describes it that way...i have a products table also

like image 202
Matt Elhotiby Avatar asked Dec 16 '22 15:12

Matt Elhotiby


1 Answers

try

$autoload['libraries'] = array('database');

in application/config/autoload.php

like image 155
Ivan Avatar answered Jan 06 '23 12:01

Ivan