本文介绍了Laravel"ErrorException(E_NOTICE)未定义变量:类"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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);
            }

        }

推荐答案

<?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);
        }

    }

这篇关于Laravel"ErrorException(E_NOTICE)未定义变量:类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:13