Laravel“ ErrorException(E_NOTICE)未定义的变量:类”

时间:2019-12-16 07:31:20

标签: php laravel eloquent sentiment-analysis

    Hello everyone can help me, I have a problem "ErrorException (E_NOTICE) Undefined variable: class"


         class ControllerTraining extends Controller
    {
        public function index()
        {

                $title = "Data Training";
                foreach(Sentimen::all() as $stm){
                    $class['class'][] = $stm->kategori;
                    $data_training[$stm->kategori] = WordFrequency::where('id_sentimen',$stm->id_sentimen)->get();
                }


                $total = WordFrequency::count();

*此处错误

 foreach($class['class'] as $cls){
                        $sum = DB::table('term_frequency')->select(DB::raw('SUM(jumlah) as jumlah_term'))->join('sentimen', 'sentimen.id_sentimen', '=', 'term_frequency.id_sentimen')->where('sentimen.kategori',$cls)->whereNotNull('id_training')->first();   
                        $data_sum[] = [
                            'kelas' => $cls,
                            'jumlah' => $sum->jumlah_term,
                        ];
                    }

                    $distinct = DB::select("SELECT count(*) as total FROM (SELECT kata FROM term_frequency WHERE term_frequency.id_training is not null GROUP by kata) as x");
                    foreach($distinct as $dst){
                        $distinctWords = $dst->total;
                    }
                    $uniqueWords = $distinctWords;


                    $i = 0;
                    foreach($class['class'] as $cls)
                    {
                        $Count = DB::table('data_training')
                                    ->join('data_crawling', 'data_training.id_crawling', '=', 'data_crawling.id_crawling')
                                    ->join('sentimen', 'sentimen.id_sentimen', '=', 'data_crawling.id_sentimen')
                                    ->select('sentimen.kategori as kategori')
                                    ->where('sentimen.kategori', '=', $cls)
                                    ->count();
                        // $Count = DataTraining::where('kategori',$cls)->count();
                        $totalCount = DataTraining::count();
                        $prior[] = [
                            'kelas' => $cls,
                            'nilai' => $Count / $totalCount,
                        ];
                    }

                    return view('data_training', compact(['title','data_sum','prior','uniqueWords','data_training','total']));


            }

            public function hapus_training($kategori)
            {
                $data_training = TwitterStream::where('id_sentimen',$kategori)->delete();
                return redirect('/training');
            }

            public function data_sentimen()
            {
                $data_training = Sentimen::all();
                return response()->json($data_training);
            }

        }

1 个答案:

答案 0 :(得分:0)

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; 
use App\Models\DataTraining; 
use App\Models\WordFrequency; 
use App\Models\Sentimen; 
use App\Models\TwitterStream; 
use Illuminate\Http\Respons;
use DB; 
    class ControllerTraining extends Controller
    {
        public function index()
        {

            $title = "Data Training";
            $sentimen = Sentimen::all();
            $class = [];
            $data_training = [];
            if(!empty($sentimen)){ 
                $class['class']= [];
                foreach($sentimen as $key => $stm){
                    $class['class'][$key] = $stm->kategori;
                    $data_training[$stm->kategori] = WordFrequency::where('id_sentimen',$stm->id_sentimen)->get();
                }

                $total = WordFrequency::count();
                foreach($class['class'] as $cls){
                    $sum = DB::table('term_frequency')->select(DB::raw('SUM(jumlah) as jumlah_term'))->join('sentimen', 'sentimen.id_sentimen', '=', 'term_frequency.id_sentimen')->where('sentimen.kategori',$cls)->whereNotNull('id_training')->first();   
                    $data_sum[] = [
                        'kelas' => $cls,
                        'jumlah' => $sum->jumlah_term,
                    ];
                }

                $distinct = DB::select("SELECT count(*) as total FROM (SELECT kata FROM term_frequency WHERE term_frequency.id_training is not null GROUP by kata) as x");
                foreach($distinct as $dst){
                    $distinctWords = $dst->total;
                }
                $uniqueWords = $distinctWords;


                $i = 0;
                foreach($class['class'] as $cls)
                {
                    $Count = DB::table('data_training')
                                ->join('data_crawling', 'data_training.id_crawling', '=', 'data_crawling.id_crawling')
                                ->join('sentimen', 'sentimen.id_sentimen', '=', 'data_crawling.id_sentimen')
                                ->select('sentimen.kategori as kategori')
                                ->where('sentimen.kategori', '=', $cls)
                                ->count();
                    // $Count = DataTraining::where('kategori',$cls)->count();
                    $totalCount = DataTraining::count();
                    $prior[] = [
                        'kelas' => $cls,
                        'nilai' => $Count / $totalCount,
                    ];
                }
                return view('data_training', compact(['title','data_sum','prior','uniqueWords','data_training','total']));
            } else {
                echo "sentimen is empty";
            }
        }

        public function hapus_training($kategori)
        {
            $data_training = TwitterStream::where('id_sentimen',$kategori)->delete();
            return redirect('/training');
        }

        public function data_sentimen()
        {
            $data_training = Sentimen::all();
            return response()->json($data_training);
        }

    }