jeudi 24 octobre 2019

Is there a way to create SQL view table in Laravel without writing Raw SQL query?

I'm trying to create a SQL view table in Laravel 5.4. I've research some ways to create the table however most of the solutions led to writing Raw SQL queries to create the table. In my case i need to join tables, compare the data in multiple data in the table and count the no of data with a certain value which i found difficult to do from Raw SQL query. Is there way to create the view table after getting data from eloquent ORM and performing the required operations and creating an array of the data structure.

$marksCount = \App\Models\StudentsClassEight::join('school_class_eight_marks as m', 'students_class_eight.id', '=', 'm.student_id')
                                                    ->join('mast_schoollist as sl', 'students_class_eight.sch_num', '=', 'sl.sch_num')
                                                    ->where('sl.sch_year', '=', $year)
                                                    ->where('students_class_eight.sch_year', '=', $year)
                                                    ->where('in_school', '=', 1)
                                                    ->where(['province_code' => $provinceCode, 'district_code' => $districtCode, 'locallevel_code' => $localLevelCode])
                                                    ->groupBy(['sch_num', 'students_class_eight.id'])
                                                    ->selectRaw(
                                                        'sl.province_code,sl.district_code,sl.locallevel_code,sl.nm_sch,students_class_eight.sch_num,students_class_eight.id,students_class_eight.sch_year,students_class_eight.caste,students_class_eight.gender,COUNT(m.practical_marks) as practical'
                                                    )
                                                    ->get();

    $data = ($marksCount->groupBy('sch_num'));
    foreach ( $data as $key => $datum ) {
        $grouped_data = $datum->groupBy('caste')->map(function($mapped_data){
            return $mapped_data->groupBy('gender');
        });

        $total_female = 0;
        $total_male = 0;
        $total = 0;

        foreach($grouped_data as $g_key => $g_value){
            $count[$key][$g_key]["Male"] = isset($g_value['Male']) ? count($g_value['Male']) : 0;
            $count[$key][$g_key]["Female"] = isset($g_value['Female']) ? count($g_value['Female']) : 0;
            $count[$key][$g_key]["Total"] = $count[$key][$g_key]["Male"] + $count[$key][$g_key]["Female"];
            $total_male = $total_male + $count[$key][$g_key]["Male"];
            $total_female = $total_female + $count[$key][$g_key]["Female"];
            $total = $total + $count[$key][$g_key]["Total"];
        }
        $count[$key]["Sum"]["Male"] = $total_male;
        $count[$key]["Sum"]["Female"] = $total_female;
        $count[$key]["Sum"]["Total"] = $total;
        $list[$key]['practical'] = 0;
        $subjects                = SchoolSubjects::where('sch_num', '=', $key)->selectRaw('count(subject_id) as count')->first('count');

        foreach ( $datum as $d ) {
            if ( $d->practical == $subjects->count ) {

                $list[$key]['practical'] += 1;
                $list[$key]['nm_sch'] = $d->nm_sch;
                $list[$key]['under'] = LocalLevel::where([
                    ["p_code", "=", $d->province_code],
                    ["d_code", "=", $d->district_code],
                    ["ll_code", "=", $d->locallevel_code]
                ])->first()->ll_name;
            }

        }
    }
    return [$list, $count];

I get the required the result in the $count variable, is there any way i could do all this and save the result in the view table so that i can next time i dont need to perform the same query but get the data from the table so the load time is reduced?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire