Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter load multiple model in same controller [closed]

These is the controller

class Dashboard extends CI_Controller{
public function __construct(){
    parent::__construct();
    $this->load->model("admin/post_model");
    $this->load->model("admin/comment_model");
}
    public function index(){
    $data['post_res'] = $this->post_model->getPost();
    $data['com_res']  = $this->post_model->getComments();
    }
 }

I cannot load 2 model in the same controller. It gives me an error

Fatal error: Call to a member function getComments() on a non-object in C:\xampp\htdocs\blog\application\controllers\ram-admin\dashboard.php on line 13

How can I possibly load the models?

Thanks you so much in advance!

like image 438
user1033600 Avatar asked Jun 06 '12 07:06

user1033600


People also ask

Can we load library in model CodeIgniter?

All of the available libraries are located in your system/libraries/ directory. In most cases, to use one of these classes involves initializing it within a controller using the following initialization method: $this->load->library('class_name');

How to Pass data from controller to view in PHP?

Since a controller writes either to view or model - you'd pass variables to view via controller. $model = new Model(); $view = new View($model); $controller = new Controller($view); // This will assign variables to view $controller->indexAction(); echo $view->render();


1 Answers

Try this

class Dashboard extends CI_Controller {
 function __construct() {
      parent::__construct(); 

    $this->load->model("admin/post_model","post_model");
    $this->load->model("admin/comment_model","comment_model");
  }

public function index(){

    $data['post_res'] = $this->post_model->getPost();
    $data['com_res']  = $this->comment_model->getComments();
}
like image 175
Query Master Avatar answered Sep 25 '22 02:09

Query Master