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

noblemfd's avatar

How to get registered student per class in the current Year

In my Laravel-5.8 application, I have these four (4) tables:

CREATE TABLE `my_classes` (
 `id` int(11) UNSIGNED NOT NULL,
 `school_id` int(11) DEFAULT NULL,
 `class_name` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
 `class_head_id` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `students` (
 `id` int(11) NOT NULL,
 `student_code` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `school_id` int(11) NOT NULL,
 `first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
 `last_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE `registrations` (
 `id` int(11) UNSIGNED NOT NULL,
 `school_id` int(11) DEFAULT NULL,
 `registration_id` int(11) DEFAULT NULL,
 `student_id` bigint(20) UNSIGNED DEFAULT NULL,
 `registered_date` date NOT NULL,
 `is_registered` tinyint(1) DEFAULT 0 COMMENT '0=No,1=Yes',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `registration_types` (
 `id` int(11) UNSIGNED NOT NULL,
 `school_id` int(11) DEFAULT NULL,
 `registration_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

For each table, id is the Primary Key and it's auto-increment. Each class have one class head that is also a student. Each student belong to a class and a class has many students. Each student register yearly. There are different registrations but the compulsory on is called "Core".

See the models:

class MyClass extends Model
{
    protected $table = 'my_classes';
    protected $fillable = [
	'id',
        'school_id',
        'class_name',
        'class_head_id',
    ];

    public function classhead()
    {
        return $this->belongsTo('App\Models\ClassHead','class_head_id','id');
    } 
}
class Student extends Model
{
    protected $table = 'students';
    protected $fillable = [
	'id',
        'school_id',
        'first_name',
        'last_name',
        'class_id',
    ];
    public function myclass()
    {
        return $this->belongsTo('App\Models\MyClass', 'class_id', 'id');
    }  
}
class RegistrationType extends Model
{
    protected $table = 'registration_types';
    protected $fillable = [
	  'id',
      'school_id',
	  'registration_type',
    ];

    public function registration(){
        return $this->hasMany('App\Models\Registration');
    } 
}
class Registration extends Model
{
    protected $table = 'registrations';
    protected $fillable = [
	'id',
        'school_id',
        'is_registered',
        'student_id',
	'registration_id',
	'registration_date',
    ];

    public function student()
    {
        return $this->belongsTo('App\Models\Student','student_id');
    } 

    public function registrationtype()
    {
        return $this->belongsTo('App\Models\RegistrationType','registration_type_id');
    }
}

I have this code in the controller:

$school = Auth::user()->school_id;
$registrationTypeName = "Core";

$studentRegistrations = DB::table('my_classess AS c')
   ->leftJoin('students AS ch', function($join) use ($school)
   {
   $join->on('ch.id', '=', 'c.class_head_id')
   ->where('ch.school_id', '=', $school);
   })
   ->leftJoin('students AS s', function($join) use ($student)
   {
   $join->on('c.id', '=', 's.class_id')
   ->where('s.school_id', '=', $school);
   })
   ->leftJoin('registrations AS r', function($join) use ($school)
   {
   $join->on('s.id', '=', 'r.student_id')
   ->where('r.company_id', '=', $school)
   ->where('r.is_registered', '1');
   })
   ->join('registration_types AS rt', function($join) use ($school, $registrationTypeName)
   {
   $join->on('r.registration_type_id', '=', 'rt.id')
   ->where('rt.school_id', '=', $school)
   ->where('rt.registration_type', 'LIKE', '%'.$registrationTypeName.'%');
    })
    ->where('c.school_id', '=', $school)
    ->select(
             'c.class_name',
              DB::raw('COALESCE(CONCAT(ch.first_name, " ", ch.last_name), "None") AS class_head'),
              DB::raw('COALESCE(COUNT("r.id"), "0") AS total_registered'),
            )
            ->groupby('c.id')
            ->get();

In my controller, I want to run a query that will display all the classes, Class Head, Total Registered, Total Not Registered. If there are not records of registration for any class, it should use 0. Also,

Total Registered = COUNT("r.id") where where('r.is_registered', '1') and registration_date is current year.

Total Not Registered = Total student per class - Total Registered

Everything should look like this:

Classes | Class Head | Total Registered | Total Not Registred

I have two problems:

  1. It's giving me total student per department for Total Registered instead

  2. How do I get Total Not Registred (Total Not Registered = Total student per class - Total Registered )?

Thanks

0 likes
1 reply
noblemfd's avatar

The first part is sorted out. I changed:

DB::raw('COALESCE(COUNT("r.id"), "0") AS total_registered'),

to

DB::raw('COALESCE(COUNT(r.id), "0") AS total_registered'),

Please or to participate in this conversation.