Home  >  Q&A  >  body text

How to use model in codeigniter controller?

I'm very new to codeigniter-3 framework and I'm using a model in a controller like this $this->load->model('model_name') It's working fine, I'm trying Using the following way but it threw an error, can you help me fix this

An uncaught Exception was encountered

Type: Error

Message: Class 'models\TestModel' not found

TestController.php

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

class TestController extends CI_Controller {

    public function student(){
        $obj = new TestModel();
        $value = $obj->student_college();
        echo $value;
    }
}

P粉207969787P粉207969787287 days ago614

reply all(1)I'll reply

  • P粉976737101

    P粉9767371012024-01-30 00:33:33

    You can have a try

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class TestController extends CI_Controller {
        public function __construct() {
            parent::__construct();
            // Load model
            $this->load->model('TestModel');
        }
        public function student(){
            $obj = new TestModel();
            $value = $obj->student_college();
            echo $value;
        }
    }

    reply
    0
  • Cancelreply