Welcome Guest, Not a member yet? Register   Sign In
CI 4 will not recognize UserModel
#1

Good afternoon. I am using CodeIgniter 4 on Windows 10 and I'm having an issue where CI is not recognizing my model.
I am receiving the following error upon form submission:
Quote:
[color=var(--black-600)]Error
Class 'CodeIgniter\Models\UserModel' not found
APPPATH\Controllers\User.php at line 42
[/color]
CI is displaying this line (in User.php) as the source of the issue:
Quote:
[color=var(--black-600)]$model = new UserModel();[/color]
Here is my User controller file (app/Controllers/User.php):
Code:
<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\Models\UserModel;

class User extends Controller {
    public function index() {
      return redirect()->to('/user/signin');
    }

    public function signin() {
      $data = [];
      helper(['form']);

      echo view('templates/dashkit/head');
      echo view('templates/dashkit/signin', $data);
      echo view('templates/dashkit/foot');

      return;
    }

    public function register() {
      $data = [];
      helper(['form']);

      if($this->request->getMethod() == 'post') {
        $rules = [
          'company' => 'required|min_length[8]|is_unique[users.user_company]',
          'email' => 'required|min_length[11]|max_length[255]|valid_email|is_unique[users.user_email]',
          'firstname' => 'required|min_length[2]|max_length[50]',
          'lastname' => 'required|min_length[2]|max_length[50]',
          'password' => 'min_length[8]|max_length[255]',
          'password_confirm' => 'matches[password]',
        ];

        if(!$this->validate($rules)) {
          $data['validation'] = $this->validator;
        } else {
          // If the information passes validation, add the user to the database
          $model = new UserModel();

          $newData = [
            'company'   => $this->request->getVar('company'),
            'email'     => $this->request->getVar('email'),
            'firstname' => $this->request->getVar('firstname'),
            'lastname'  => $this->request->getVar('lastname'),
            'password'  => $this->request->getVar('password')
          ];
          $model->save($newData);
          $session = session();
          $session->setFlashdata('success', 'The form was successfully submitted.');
          return redirect()->to('/dashboard');
        }
      }

      echo view('templates/dashkit/head');
      echo view('templates/dashkit/register', $data);
      echo view('templates/dashkit/foot');

      return;
    }
}
And here is my UserModel file (app/Models/UserModel.php):
Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model {
  protected $allowedFields = ['user_company', 'user_created_at', 'user_email', 'user_firstname', 'user_lastname', 'user_password'];
  protected $beforeInsert = ['beforeInsert'];
  protected $beforeUpdate = ['beforeUpdate'];
  protected $createdField  = 'created_at';
  protected $table = 'users';

  protected function beforeInsert(array $data) {
    if(isset($data['data']['password'])) {
      $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
    }

    return $data;
  }

  protected function beforeUpdate(array $data) {


    return $data;
  }
}


Thanks for your help!
Reply
#2

Did you save it with the characters upper case UserModel or lowercase usermodel?

Case is very important here. Also check your base_url should end with a /
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Change the use import of the Model.

from CodeIgniter\Models\UserModel

to App\Models\UserModel
Reply




Theme © iAndrew 2016 - Forum software by © MyBB