Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined property '$M_Auth'.intelephense(1014) on Codeigniter 4

I’m going to make login page, but it keep telling me Undefined property '$M_Auth' when I’m trying to define my model, it’s work well in my last project, but now when I open the last project this message is also occurred.

This is my Auth Controller

<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\M_Auth;

class Auth extends BaseController
{
    public function __construct()
    {
        $this->M_Auth = new M_Auth();
    }
    public function index(){
        echo form_open();
        return view('Auth/login');
    }

    public function cek_login(){
        if($this->validate([
            'username' => [
                'label' => 'Nama Pengguna',
                'rules' => 'required',
                'errors' =>[
                    'required' => '{field} tidak boleh kosong!'
                ]
            ],
            'password' => [
                'label' => 'Kata Sandi',
                'rules' => 'required',
                'errors' =>[
                    'required' => '{field} tidak boleh kosong!'
                ]
            ] 
        ])){
            //jika valid
            $username = $this->request->getPost('username');
            $password = $this->request->getPost('password');
            $cek = $this->M_Auth->login($username, $password);
        }else{

        }
    }

}

This is M_Auth model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Models;

use CodeIgniter\Model;

class M_Auth extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'pengguna';
    protected $primaryKey       = 'id_pengguna';
    protected $useAutoIncrement = true;
    protected $allowedFields    = ['level_id', 'username', 'password'];

    public function login($username, $password){
        return $this->db->table('pengguna')->where([
            'username' => $username,
            'password' => $password
        ])->get()->getRowArray();
    }

}

Idk what’s wrong, tried to fix it by watch tutorial on ytb but it doesn’t have any different than my code and already search here too but i don’t really understand the problem

>Solution :

...
class Auth extends BaseController
{
    private M_Auth $M_Auth;
    ^^^^^^^^^^^^^^^^^^^^^^
    public function __construct()
    {
        $this->M_Auth = new M_Auth();
    }
    public function index(){
        echo form_open();
        return view('Auth/login');
    }
   ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading