ErrorException(E_NOTICE)试图获取非对象的属性“ sentimen”

时间:2019-12-17 07:03:23

标签: php laravel

帮助,我收到一条错误消息ErrorException(E_NOTICE)试图获取非对象的属性“ sentimen”

    public function prediksi()
        {
            $collection = array();
            $title = "Data Prediksi Sentimen";
            $testing_data = DataTesting::count();
            $klasifikasi = DataTesting::with(['data_crawling','klasifikasi'])->get();
            foreach($klasifikasi as $class){
                $prediksi = Klasifikasi::with(['sentimen'])->where('id_testing',$class->id_testing)->first();
                $hasil = Hasil::where('id_testing',$class->id_testing)->get();
                $aktual = Sentimen::where('id_sentimen', $class->data_crawling->id_sentimen)->first();
                $collection[] = [
                    'id_testing' => $class->id_testing,
                    'username' => $class->data_crawling->username,
                    'tweet' => $class->data_crawling->tweet,
                    'kategori' => $aktual->kategori,
                    'prediksi' => $prediksi->sentimen->kategori,
                ];
            }

            return view('visualisasi.prediksi', compact(['title','collection','testing_data','hasil']));
        }

View Code

1 个答案:

答案 0 :(得分:1)

认为您的查询和关系正确无误,则可以在下面使用 IF()条件

public function prediksi()
    {
        $collection = array();
        $title = "Data Prediksi Sentimen";
        $testing_data = DataTesting::count();
        $klasifikasi = DataTesting::with(['data_crawling','klasifikasi'])->get();
        foreach($klasifikasi as $class){
            $prediksi = Klasifikasi::with(['sentimen'])->where('id_testing',$class->id_testing)->first();
            $hasil = Hasil::where('id_testing',$class->id_testing)->get();
            $aktual = Sentimen::where('id_sentimen', $class->data_crawling->id_sentimen)->first();
            if($prediksi && isset($prediksi->sentimen)){
                  $collection[] = [
                'id_testing' => $class->id_testing,
                'username' => $class->data_crawling->username,
                'tweet' => $class->data_crawling->tweet,
                'kategori' => $aktual->kategori,
                'prediksi' => $prediksi->sentimen->kategori,
            ];
            }
        }

        return view('visualisasi.prediksi', compact(['title','collection','testing_data','hasil']));
    }

已更新

public function prediksi()
        {
            $collection = array();
            $title = "Data Prediksi Sentimen";
            $testing_data = DataTesting::count();
            $klasifikasi = DataTesting::with(['data_crawling','klasifikasi'])->get();
            foreach($klasifikasi as $class){
                $prediksi = Klasifikasi::with(['sentimen'])->where('id_testing',$class->id_testing)->first();
                $hasil = Hasil::where('id_testing',$class->id_testing)->get();
                $aktual = Sentimen::where('id_sentimen', $class->data_crawling->id_sentimen)->first();
                if($prediksi && isset($prediksi->sentimen)){
                      $collection[] = [
                    'id_testing' => $class->id_testing,
                    'username' => $class->data_crawling->username,
                    'tweet' => $class->data_crawling->tweet,
                    'kategori' => $aktual->kategori,
                    'prediksi' => $prediksi->sentimen->kategori,
                ];
                 $hasil_data[$class->id_testing][] = $hasil;
                }
            }

            return view('visualisasi.prediksi', compact(['title','collection','testing_data','hasil_data']));
        }

然后在每次查看数据时在视图中显示

foreach($collection as $key){

  //to use hasil data
  foreach($hasil_data[$key['id_testing']] as $hasil){
     dd($hasil);

  }
}