Blog of King Software

Learn online with us in simple steps.

How to solve "CodeIgniter PHP Model Access Unable to locate the model you have specifie" error in PHP?


Created by Bhavesh Gohil & Panakaj Anarse on Date: 26/07/2022

What is a Model?

Models are PHP classes that are designed to work with information in your database. For example, let’s say you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update and retrieve your blog data.

Anatomy of a Model

Model classes are stored in your application/models/directory. They can be nested within sub-directories if you want this type of organization.

The basic prototype for a model class is this:

class Model_name extends CI_Model {
}


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 and you have to care to use everywhere the same name

Loading a Model

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

  • $this->load->model('model_name');

  • If your model is located in a sub-directory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:

    $this->load->model('blog/queries');

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

    $this->load->model('model_name');

    $this->model_name->method();

  • If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method:

    $this->load->model('model_name', 'foobar');

    $this->foobar->method();

If you start the model name in lower case localhost works fine but online it doesn't and they give below error.

CodeIgniter PHP Model Access "Unable to locate the model you have specified" codeigniter-model-error


Solution is as below

Make sure:

  • First letter uppercase.
  • Class name exact name as file name and have to care to use everywhere the same name.
  • Make sure your file ends with .php extension.

For example for Login_m you have to:
  • Class names must have the first letter capitalized with the rest of the name lowercase and Make sure your class extends the base Model class.

  • modelname

  • The file name must match the class name. For example, if this is your class:

    application/models/Login_m.php

  • Your models will usually be loaded with the same name as the class name from your controller methods. To load the model you will use the following method:

  • same-name-use