Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mickie's avatar

count total students in each class & section and show in blade

I have students table with data of students and class_id and section_id saved in 'students' table. i need to show count of students in each class and section from 'students' table in this format. P.S. some classes may have 4 or 5 sections and some may have 1, 2 or 3.

Class  J 	R 	D 	A 	TOTAL
10 		9 	6 	7 	10 	32
09 		16 	6 	7 	15 	44
08 		13 	5 	3 	19 	40
07 		9 	16 	16 	13 	54
06 		12 	9 	8 	7 	36
05 		9 	7 	6 	8 	30
04 		8 	12 	5 	9 	34
03 		9 	7 	5 	14 	35

Class and Sections Table as follows:-

public function up()
{
    Schema::create('class', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('name', 50);
        $table->string('full_name', 50);
        $table->boolean('status')->default(1);
        $table->timestamps();        
    });
}

 public function up()
    {
        Schema::create('sections', function (Blueprint $table) {
            $table->integer('id',true,true);
            $table->string('name', 50);
            $table->string('short_name', 50); 
            $table->boolean('status')->default(1);
            $table->timestamps();
        });
    }

using this code to fetch all classes and sections

        $classes = Clas::where('status','1')
                    ->get();
        $sections = Section::where('status','1')
                    ->get();

i managed to get the same output by using this code in blade file (Two for each loops for class and sections and then fetch count for each class and section)

<thead class="bg-light">
    <tr>
        <th width="60">Class</th>
        @if($sections)
            @foreach($sections as $section)
                <th width="60">{{  substr($section->name, 0, 1) }}</th>
            @endforeach
        <th width="60">TOTAL</th>
        @endif                                    
    </tr>
</thead>
    <tbody>               
    @foreach($classes as $class)
        @php $total = 0; @endphp
        <tr>
            <td> {{ $class->name }}</td>                                    
        @foreach($sections as $section)                            
            @php
            $result = \Illuminate\Support\Facades\DB::table('students AS s')
                ->select(DB::raw("count(s.id) as count"))
                ->rightJoin('class AS c', 'c.id', '=', 's.clas_id')
                ->rightJoin('sections AS sec', 'sec.id', '=', 's.section_id')
                ->whereNull('s.rolloff')
                ->where('c.id',$class->id)
                ->where('sec.id',$section->id)
                ->groupBy('c.name','sec.name')
                ->get();
            $total += !empty($result->first()->count) ? $result->first()->count : 0;
            @endphp
            <td width="60">{{ !empty($result->first()->count) ? $result->first()->count : '' }}</td>
        @endforeach
            <td width="60">{{ $total }}</td>
        </tr> 
    @endforeach
        </tbody>

Is it possible to get the same output by fetching data in controller and pass to view? and instead of multiple queries with for each loop using less query to get multiple counts.

Thanks.

0 likes
0 replies

Please or to participate in this conversation.