Bagaimana cara mengatasi error seperti ini?

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$model_barang

Filename: controllers/Welcome.php

Line Number: 23

Backtrace:

File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/application/controllers/Welcome.php

Line: 23

Function: _error_handler

File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/index.php

Line: 315

Function: require_once

An uncaught Exception was encountered

Type: Error

Message: Call to a member function tampil_data() on null

Filename: /home/vol18_2/epizy.com/epiz_28896231/htdocs/application/controllers/Welcome.php

Line Number: 23

Backtrace:

File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/index.php

Line: 315

Function: require_once

INI UNTUK WELCOME.PHP

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

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $data['barang'] = $this->model_barang->tampil_data()->result();
        $this->load->view('templates/header');
        $this->load->view('templates/sidebar');
        $this->load->view('dashboard',$data);
        $this->load->view('templates/footer');
    }
}

LALU INI KODING UNTUK MODEL_BARANG.PHP

<?php

class Model_barang extends CI_Model{
    public function tampil_data(){
        return $this->db->get('tb_barang');
    }
    public function tambah_barang($data,$table){
        $this->db->insert($table,$data);
    }
    public function edit_barang($where,$table){
        return $this->db->get_where($table,$where);
    }
    public function update_data($where,$data,$table)
    {
        $this->db->where($where);
        $this->db->update($table,$data);
    }
    public function hapus_data($where,$table){
        $this->db->where($where);
        $this->db->delete($table);
    }

    public function find($id)
    {
        $result =$this->db-> where('id_brg',$id)
                          -> limit(1)
                          -> get('tb_barang');
    if($result->num_rows()> 0){
        return $result->row();
    }else{
        return array();

        }

    }

    public function detail_brg($id_brg)
    {
        $result = $this->db->where('id_brg',$id_brg)->get('tb_barang');
        if($result->num_rows() >0){
            return $result->result();
        }else{
            return false;
        }
    }

}
avatar laurentellyzabeth
@laurentellyzabeth

14 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

Tanggapan

Sesuaikan judul dengan pertanyaan kamu ya. Baca aturan main cara membuat judul yang baik

3 Jawaban:

<div>Pada welcome controller, tambahkan script untuk me-load file model_barang gini :</div><pre>public function __construct() { parent::__construct(); $this-&gt;load-&gt;model(['Model_barang' =&gt; 'model_barang'); }</pre><div><br>Silahkan cek referensinya disini&nbsp;<a href="https://codeigniter.com/userguide3/general/models.html#loading-a-model">https://codeigniter.com/userguide3/general/models.html#loading-a-model</a></div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Tanggapan

tetap sama hasilnya gan

<div>Coba method index-nya di ubah seperti ini :</div><pre>public function index() { //Load model $this-&gt;load-&gt;model('Model_barang'); $data['barang'] = $this-&gt;Model_barang-&gt;tampil_data()-&gt;result(); $this-&gt;load-&gt;view('templates/header'); $this-&gt;load-&gt;view('templates/sidebar'); $this-&gt;load-&gt;view('dashboard',$data); $this-&gt;load-&gt;view('templates/footer'); }</pre><div><br>Perhatikan penamaan-nya, model_barang dan Model_barang, pastikan menggunakan huruf yg sama, yaitu capitalize di awal nama class modelnya.</div><div><br></div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Tanggapan

nama class modelnya Model_barang namun tetap sama errornya

<pre>public function index() { $this-&gt;load-&gt;model('model_barang'); $data['barang'] = $this-&gt;model_barang-&gt;tampil_data()-&gt;result(); $this-&gt;load-&gt;view('templates/header'); $this-&gt;load-&gt;view('templates/sidebar'); $this-&gt;load-&gt;view('dashboard',$data); $this-&gt;load-&gt;view('templates/footer'); }</pre><div>&nbsp;kalau dibikin seperti ini sudah bisa gan tapi tampilan webnya jadi berantakan <br><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/jpeg&quot;,&quot;filename&quot;:&quot;ss an.JPG&quot;,&quot;filesize&quot;:50830,&quot;height&quot;:640,&quot;url&quot;:&quot;https://i.ibb.co/kx8Wsk5/ss-an.jpg&quot;,&quot;width&quot;:1011}" data-trix-content-type="image/jpeg" data-trix-attributes="{&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--jpg"><img src="https://i.ibb.co/kx8Wsk5/ss-an.jpg" width="1011" height="640"><figcaption class="attachment__caption"><span class="attachment__name">ss an.JPG</span></figcaption></figure>solusinya bagaimana ya gan?</div>

avatar laurentellyzabeth
@laurentellyzabeth

14 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

Tanggapan

Tinggal ganti aja base_url nya

Login untuk ikut Jawaban